Memory: Free | Index | Disk: Loader

Write Your Own OS from Scratch - Chapter 3. Memory

Memory: Resize

In mem.asm we add the code to do the memory resize this reuses part of allocate.

;-------------------------------------------------------
; Memory Resize
; Input:
;      es = allocated memory
;      bx = new size
; Output:
;      Carry set if error
;      Carry clear if success
;-------------------------------------------------------
memresize:
        push    cx                                      ; Save registers
        push    ds
        mov     ax, es                                  ; Get allocated space
        dec     ax                                      ; Adjust to arena
        mov     ds, ax                                  ; Set DS to arena
        call    memchecksignature                       ; Check valid
        jc      memallocerror
        call    memmergefree                            ; Merge with free if available
        jc      memallocerror
        mov     cx,[ds:memarena.size]                   ; Get new size
        cmp     bx, cx                                  ; Compare size to requested size
        mov     ax, ds                                  ; memallocmake requires seg in ax
        jbe     memallocmake                            ; jump to the end of the alloc routine
        jmp     memallocerror                           ; Otherwise error
		

mem.asm

;=======================================================
; Memory - mem.asm
; -----------------------------------------------------
; Written in NASM
; Written by Marcus Kelly
;=======================================================
struc memarena
    .signature  resb 1
    .owner      resw 1
    .size       resw 1
    .reserved   resb 3
    .name       resb 8
endstruc

memfirst:       dw      0
membest:        dw      0
memlargest:     dw      0
memlast:        dw      0
memlargestsize: dw      0
memmode:        db      0

;-------------------------------------------------------
; Memory Initialize
; Input:
;      ds = mcb first block
;      bx = size
; Output:
;      None
;-------------------------------------------------------
meminit:
    push   ax                                           ; Save registers
    push   cx
    mov    al, 'Z'                                      ; Set arena signature to end
    mov    [ds:memarena.signature], al
    mov    [ds:memarena.size], bx                       ; Set arena size
    xor    ax, ax                                       ; Set arena owner
    mov    [ds:memarena.owner], ax
    mov    [cs:memmode], byte 1                         ; Memory mode allocate first
    pop    cx                                           ; Restore registers
    pop    ax
    ret                                                 ; Return from subroutine

;-------------------------------------------------------
; Memory Allocate
; Input:
;      BX = number of paragraphs to allocate
; Output:
;      CF clear if successful
;      AX = segment of allocated block
;      CF set on error
;      AX = error code (07h,08h) (Not Implemented)
;      BX = size of largest available block
;-------------------------------------------------------
memalloc:
    push   cx                                           ; Save registers
    push   ds
    xor    ax, ax                                       ; Reset found arenas to zero
    mov    [cs:memfirst], ax
    mov    [cs:membest], ax
    mov    [cs:memlargest], ax
    mov    [cs:memlast], ax
    mov    ax, [cs:mcbfirst]                            ; Get first arena
    mov    ds, ax
memallocnext:
    call   memchecksignature                            ; Check valid arena
    jc     memallocerror
    mov    ax, [ds:memarena.owner]                      ; Check if owned
    or     ax, ax
    jnz    memallocinc                                  ;    If owned goto next arena
    call   memmergefree                                 ; Not owned merge next arena if free
    jc     memallocerror
    mov    cx, [ds:memarena.size]                       ; Check if big enough
    cmp    bx, cx                                       ; If request is bigger in size
    jg     memallocinc                                  ;   If not big enough goto next arena
    cmp    [cs:memfirst], word 0                        ; Check if first found is set
    jne    memallocbest
    mov    [cs:memfirst], ds                            ; If not set first found
memallocbest:
    cmp    [cs:membest], word 0                         ; Check if best found is set
    jz     memallocsbest
    cmp    [cs:membest], cx                             ; See if this arena is smaller
    jl     memalloclargest
memallocsbest:
    mov    [cs:membest], ds                             ; If smaller set new best
memalloclargest:
    cmp    [cs:memlargest], word 0                      ; Check if largest found is set
    jz     memallocslargest
    cmp    [cs:memlargest], cx                          ; See if this arena is bigger
    jg     memalloclast
memallocslargest:
    mov    [cs:memlargest], ds                          ; Set largest found   
    mov    [cs:memlargestsize], cx                      ; Set largest available size
memalloclast:
    mov    [cs:memlast], ds                             ; Always set last found
memallocinc:
    cmp    [ds:memarena.signature], byte 'Z'            ; Check if arena is the last block
    jz     memallocdone                                 ;   If it is we are done
    call   memarenanext                                 ; Point to next arena
    jc     memallocerror
    jmp    memallocnext                                 ; Process next arena
memallocdone:
    cmp    [cs:memmode], byte 1                         ; If mode = 1 use memfirst
    jnz     memallocnfirst
    mov    ax, [cs:memfirst]
    jmp    memallocmake
memallocnfirst:
    cmp    [cs:memmode], byte 0                         ; If mode = 0 use membest
    jnz    memallocnbest
    mov    ax, [cs:membest]
    jmp    memallocmake
memallocnbest:
    cmp    [cs:memmode], byte 2                         ; If mode = 2 use memlargest
    jnz    memallocnlargest
    mov    ax, [cs:memlargest]
    jmp    memallocmake
memallocnlargest:
    mov    ax, [cs:memlast]                             ; Else use memlast
memallocmake:
    mov    ds, ax                                       ; Point to selected arena  
    mov    cx, [ds:memarena.size]                       ; Get size of arena
    sub    cx, bx                                       ; Subtract request size
    jz     memallocset
    add    ax, bx                                       ; Add size +1 to arena
    inc    ax                                           ;   for next arena
    push   es                                           ; Save ES
    mov    es, ax                                       ; Point to next arena
    dec    cx                                           ; Add a new arena
    mov    [es:memarena.size], cx                       ; Set size of next arena
    mov    [ds:memarena.size], bx                       ; Set allocated arena size
    mov    al, [ds:memarena.signature]                  ; Move signature from this arena
    mov    [es:memarena.signature], al                  ;    to the next arena
    mov    [ds:memarena.signature], byte 'M'            ; Set this arenas signature
    mov    [es:memarena.owner], word 0                  ; Make sure next arena owner is none
    pop    es                                           ; Restore ES
memallocset:
    mov    ax, ds                                       ; Get allocated arena
    inc    ax                                           ; Allocated page starts right after arena
    mov    [ds:memarena.owner], ax
    sub    [cs:memlargestsize], bx                      ; Subtract request size form largest
    mov    bx, [cs:memlargestsize]                      ; BX = largest allocation size
    pop    ds                                           ; Restore registers
    pop    cx
    clc                                                 ; Clear carry for success
    ret                                                 ; Return from subroutine
memallocerror:
    pop    ds                                           ; Restore registers
    pop    cx
    stc                                                 ; Set carry for error
    ret                                                 ; Return from subroutine

;-------------------------------------------------------
; Memory Resize
; Input:
;      es = allocated memory
;      bx = new size
; Output:
;      Carry set if error
;      Carry clear if success
;-------------------------------------------------------
memresize:
        push    cx                                      ; Save registers
        push    ds
        mov     ax, es                                  ; Get allocated space
        dec     ax                                      ; Adjust to arena
        mov     ds, ax                                  ; Set DS to arena
        call    memchecksignature                       ; Check valid
        jc      memallocerror
        call    memmergefree                            ; Merge with free if available
        jc      memallocerror
        mov     cx,[ds:memarena.size]                   ; Get new size
        cmp     bx, cx                                  ; Compare size to requested size
        mov     ax, ds                                  ; memallocmake requires seg in ax
        jbe     memallocmake                            ; jump to the end of the alloc routine
        jmp     memallocerror                           ; Otherwise error

;-------------------------------------------------------
; Memory Free
; Input:
;      ES = Allocated Segment
; Output:
;      Carry clear on success
;      Carry set on fail
;-------------------------------------------------------
memfree:
    push   ax                                           ; Save registers
    push   ds
    mov    ax, es                                       ; Get arena memory block to free
    dec    ax                                           ; Decrement for arena
    mov    ds, ax                                       ; Set ds for check signature 
    call   memchecksignature                            ; Validate arena
    jc     memfreefail
    mov    [ds:memarena.owner], word 0                  ; Free arena by setting owner to zero
    pop    ds                                           ; Restore registers
    pop    ax
    clc                                                 ; Clear carry for success
    ret                                                 ; Return from subroutine
memfreefail:
    pop    ds                                           ; Restore registers
    pop    ax
    stc                                                 ; Set carry for fail
    ret                                                 ; Return from subroutine

;-------------------------------------------------------
; Memory Merge Free
; Input:
;      DS = Arena
; Output:
;      None
;-------------------------------------------------------
memmergefree:
    push   cx                                            ; Save registers
    push   es
memmergefreenext:
    cmp    [ds:memarena.signature], byte 'Z'             ; If last block don't attempt merge
    jz     memmergefreedone
    push   ds                                            ; Save current arena
    call   memarenanext                                  ; Get next arena
    jc     memmergefreepop
    push   ds                                            ; ES = Next arena
    pop    es
    pop    ds                                            ; Restore current arena
    cmp    [es:memarena.owner], word 0                   ; Check if next arena is free
    jnz    memmergefreedone                              ; If not don't merge
    mov    cx, [es:memarena.size]                        ; Get next arena size
    inc    cx                                            ; add 1 for arena erase
    add    [ds:memarena.size], cx                        ; add to current arena size
    mov    cl, [es:memarena.signature]                   ; Copy signature backwards
    mov    [ds:memarena.signature], cl                   ;   incase it is 'Z'
    jmp    memmergefreenext                              ; Try another merge
memmergefreepop:
    pop    ds                                            ; We were done before pop do it now
memmergefreedone:
    pop    es                                            ; Restore registers
    pop    cx
    ret                                                  ; Return from subroutine

;-------------------------------------------------------
; Memory Next Arena
; Input:
;      DS = Arena
; Output:
;      DS = Next Arena
;-------------------------------------------------------
memarenanext:
    push   ax                                         ; Save AX
    mov    ax, ds                                     ; Get current arena
    add    ax, [ds:memarena.size]                     ; Add arena size
    inc    ax                                         ; Increment past end of alloc
    mov    ds, ax                                     ; Set new arena
    pop    ax                                         ; Restore AX
                                                      ; Drop into check signature
;-------------------------------------------------------
; Memory Check Signature
; Input:
;      DS = Arena
; Output:
;      Carry Set = Not Valid
;      Carry Clear = Valid
;-------------------------------------------------------
memchecksignature:
    cmp    [ds:memarena.signature], byte 'M'            ; Check if signature M
    jz     memchecksignaturevalid                       ;   if yes then valid
    cmp    [ds:memarena.signature], byte 'Z'            ; Check if signature Z
    jz     memchecksignaturevalid                       ;   if yes then valid
    stc                                                 ; Set carry not valid
    ret                                                 ; Return from subroutine
memchecksignaturevalid:
    clc                                                 ; Clear carry valid
    ret                                                 ; Return from subroutine
		

Service

In int.asm inside the inthook routine we add the code to call the resize routine.

    cmp    ah, 04Ah                                     ; Service 4Ah: resize
    jnz    inthookdone4A
    call   memresize
    jmp    inthookdone
inthookdone4A:
		

int.asm

;=======================================================
; Service - int.asm
; -----------------------------------------------------
; Written in NASM
; Written by Marcus Kelly
;=======================================================

;-------------------------------------------------------
; Interrupt Hook
;-------------------------------------------------------
inthook:
    cmp    ah, 002h                                     ; Service 02h: stdout
    jnz    inthookdone02
    call   stdout
    jmp    inthookdone
inthookdone02:
    cmp    ah, 008h                                     ; Service 08h: stdin
    jnz    inthookdone08
    call   stdin
    jmp    inthookdone
inthookdone08:
    cmp    ah, 009h                                     ; Service 09h: strout
    jnz    inthookdone09
    call   strout
    jmp    inthookdone
inthookdone09:
    cmp    ah, 00Ah                                     ; Service 0Ah: strin
    jnz    inthookdone0a
    call   strin
    jmp    inthookdone
inthookdone0a:
    cmp    ah, 025h                                     ; Service 25h: setint
    jnz    inthookdone25
    call   intset
    jmp    inthookdone
inthookdone25:
    cmp    ah, 048h                                     ; Service 48h: alloc
    jnz    inthookdone48
    call   memalloc
    jmp    inthookdone
inthookdone48:
    cmp    ah, 049h                                     ; Service 49h: free
    jnz    inthookdone49
    call   memfree
    jmp    inthookdone
inthookdone49:
    cmp    ah, 04Ah                                     ; Service 4Ah: resize
    jnz    inthookdone4A
    call   memresize
    jmp    inthookdone
inthookdone4A:
inthookdone:
    iret

;-------------------------------------------------------
; Interrupt Set
; Input:
;       AL = Interrupt Number
;       DS = Segment of Interrupt
;       DX = Offset of Interrupt
; Output:
;       None
;-------------------------------------------------------
intset:
    push   ax                                           ; Save registers
    push   cx
    push   di
    push   es
    xor    ah, ah                                       ; DI = INT << 2
    mov    cl, 2                                        ; Essentially multiply by 4
    shl    ax, cl
    mov    di, ax                                       ; di is the address of our int
    xor    ax, ax                                       ; ES = 0 The int table is at 00000h
    mov    es, ax
    mov    ax, dx                                       ; Store offset
    stosw
    mov    ax, ds                                       ; Store segment
    stosw
    pop    es                                           ; Restore registers
    pop    di
    pop    cx
    pop    ax
    ret                                                 ; Return from subroutine

;-------------------------------------------------------
; Interrupt Initialize
; Input:
;       None
; Output:
;       DX, AX are destroyped
;-------------------------------------------------------
intinit:
    mov    dx, inthook                                  ; Offset to our service routine
                                                        ;    DS should already be the
                                                        ;    correct segment
    mov    al, 021h                                     ; Service 21h
    call   intset                                       ; Set Interrupt 21h
    ret                                                 ; Return from subroutine
		

Command

First we add the string to match.

resizestr:      db      "RESIZE", 0
		

Next we ad the resize command code.

;-------------------------------------------------------
; Memory Resize Command
; Input:
;      Command line hex number
; Output:
;       None
;-------------------------------------------------------
resize:
    push   ax                                           ; Save registers
    push   si
    push   es
    mov    si, buffer+9                                 ; Get buffer + 8 point to hex input
    call   hexwordin                                    ; Get hex input
    mov    es, ax                                       ; Free page
    mov    si, buffer+14
    call   hexwordin                                    ; Get hex input
    mov    bx, ax
    mov    ah, 04Ah                                     ; Service Resize
    int    021h
    jc     resizeerror
resizeerror:
    pop    es
    pop    si                                           ; Restore registers
    pop    ax
    ret                                                 ; Return from subroutine

		

Then we add the code to check for our command in main.

    mov    si, resizestr                                ; Point to command text
    call   cmpcmd                                       ; Compare
    jc     cmdnresize                                   ; Carry set if not equal
    call   resize                                       ; Call function
    jmp    main                                         ; We're done checking
cmdnresize:
		

And finally change:

version:        db      00Dh, 00Ah, "DEMO OS V0.10"     ; Message to be printed
                db      00Dh, 00Ah, '$'
		

to

version:        db      00Dh, 00Ah, "DEMO OS V0.11"     ; Message to be printed
                db      00Dh, 00Ah, '$'
		

demoos.asm

;=======================================================
; Demo OS - demoos.asm
; -----------------------------------------------------
; Written in NASM
; Written by Marcus Kelly
;=======================================================

    org     00000h                                      ; Assemble code for this offset

start:
    cli                                                 ; Turn off interrupts
    cld							; Make sure we are going forward
    mov     ax, cs                                      ; Set all segments to code segment
    mov     ds, ax                                      ; Make sure data segment is zero
    mov     es, ax                                      ; Make sure extra segment is zero
    mov     ss, ax                                      ; Make sure stack segment is zero
    mov     sp, 07C00h                                  ; Place stack ptr before boot sector
    sti                                                 ; Turn on interrupts

    call   intinit                                      ; Initialize interrupts
    push   ds                                           ; Save DS
    mov    ax, 00FFFh                                   ; Set first MCB
    mov    [mcbfirst], ax                               ; Save first MCB
    mov    ds, ax
    mov    bx, 07000h                                   ; Size to initialize
    call   meminit                                      ; Initialize memory
    pop    ds                                           ; Restore DS
    call   ver

main:
    call   crlf                                         ; Print carriage return / line feed
    mov    ah, 002h                                     ; Service standard output
    mov    dl, '>'                                      ; Print '>'
    int    021h
    mov    dl, ' '                                      ; Print Space
    int    021h
    mov    ah, 00Ah                                     ; Service string input
    mov    dx, buffer                                   ; Set dx to our buffer
    int    021h
    call   crlf                                         ; Print carriage return / line feed

    mov    di, buffer+2                                 ; Point to the string we typed

    mov    si, verstr                                   ; Point to command text
    call   cmpcmd                                       ; Compare
    jc     cmdnver                                      ; Carry set if not equal
    call   ver                                          ; Call function
    jmp    main                                         ; We're done checking
cmdnver:

    mov    si, clsstr                                   ; Point to command text
    call   cmpcmd                                       ; Compare
    jc     cmdncls                                      ; Carry set if not equal
    call   cls                                          ; Call function
    jmp    main                                         ; We're done checking
cmdncls:

    mov    si, echostr                                  ; Point to command text
    call   cmpcmd                                       ; Compare
    jc     cmdnecho                                     ; Carry set if not equal
    call   echo                                         ; Call function
    jmp    main                                         ; We're done checking
cmdnecho:

    mov    si, mcbstr                                   ; Point to command text
    call   cmpcmd                                       ; Compare
    jc     cmdnmcb                                      ; Carry set if not equal
    call   mcb                                          ; Call function
    jmp    main                                         ; We're done checking
cmdnmcb:

    mov    si, allocstr                                 ; Point to command text
    call   cmpcmd                                       ; Compare
    jc     cmdnalloc                                    ; Carry set if not equal
    call   alloc                                        ; Call function
    jmp    main                                         ; We're done checking
cmdnalloc:

    mov    si, freestr                                  ; Point to command text
    call   cmpcmd                                       ; Compare
    jc     cmdnfree                                     ; Carry set if not equal
    call   free                                         ; Call function
    jmp    main                                         ; We're done checking
cmdnfree:

    mov    si, resizestr                                ; Point to command text
    call   cmpcmd                                       ; Compare
    jc     cmdnresize                                   ; Carry set if not equal
    call   resize                                       ; Call function
    jmp    main                                         ; We're done checking
cmdnresize:


    mov    ah, 009h                                     ; Service Print String
    mov    dx, invalid                                  ; Invalid command
    int    021h                                         ; Our Service
    call   crlf                                         ; New line
    jmp    main                                         ; Repeat forever

;-------------------------------------------------------
; Compare Command
; Input:
;      DS:SI = Command String
;      ES:DI = Buffer String
; Output:
;      Carry set if not equal
;      Carry clear if equal
;-------------------------------------------------------
cmpcmd:
    push   ax                                           ; Save registers
    push   si
    push   di
cmpcmdnext:
    mov    al, [es:di]                                  ; Load char from command line 
    inc    di                                           ; Point to next character
    call   toup                                         ; Convert to uppercase
    mov    ah, [ds:si]                                  ; Load char to test against
    inc    si                                           ; Point to next character
    cmp    ah, al                                       ; Compare both characters
    jz     cmpcmdnext                                   ; If they are the same check next
    dec    si                                           ; Adjust to point to last char
    dec    di
    cmp    [ds:si], byte 0                              ; Our test command end should be 0
    jnz    cmpcmdne                                     ; If it isn't not our command
    cmp    [es:di], byte ' '                            ; Our input cmd might be space
    jz     cmpcmde                                      ; If it is this is our command
    cmp    [es:di], byte 00Dh                           ; Our input cmd might be CR
    jz     cmpcmde                                      ; If it is this is our command
    jmp    cmpcmdne                                     ; Otherwise not our command
cmpcmde:
    pop    di                                           ; Restore registers
    pop    si
    pop    ax
    clc                                                 ; Clear carry for match
    ret                                                 ; Return from subroutine
cmpcmdne:
    pop    di                                           ; Restore registers
    pop    si
    pop    ax
    stc                                                 ; Set carry if not a match
    ret                                                 ; Return from subroutine

;-------------------------------------------------------
; Version Command
; Input:
;      None
; Output:
;      AX, DX are Destroyed
;-------------------------------------------------------
ver:
    mov    ah, 009h                                     ; Service string output
    mov    dx, version                                  ; dx is set to offset version
    int    021h                                         ; Call the service
    ret

;-------------------------------------------------------
; Clear Screen Command
; Input:
;      None
; Output:
;      AX is Destroyed
;-------------------------------------------------------
cls:
    mov    ah, 00Fh                                     ; Get video mode
    int    010h
    mov    ah, 000h                                     ; Set video mode
    int    010h                                         ;  this automatically clears the screen
    ret

;-------------------------------------------------------
; Echo Command
; Input:
;      None
; Output:
;      None
;-------------------------------------------------------
echo:
    push   ax                                           ; Save registers
    push   dx
    push   di
    xor    ah, ah                                       ; Clear upper part of ax
    mov    al, [buffer+1]                               ; Get count from buffer
    mov    di, buffer+2                                 ; Get start of buffer
    add    di, ax                                       ; Add for end of buffer
    mov    al, '$'                                      ; Place '$' at end for printing
    mov    [di], al
    mov    ah, 009h                                     ; Service print string
    mov    dx, buffer+7                                 ; Get start of string to print
    int    021h                                         ;    7 to exclude echo
    call   crlf
    pop    di                                           ; Restore regsiters
    pop    dx
    pop    ax
    ret                                                 ; Return from subroutine

;-------------------------------------------------------
; Memory Control Block Command
; Input:
;      None
; Output:
;      AX is Destroyed
;-------------------------------------------------------
mcb:
    push   ax                                           ; Save Registers
    push   dx
    push   ds
    call   crlf                                         ; Print CR / LF
    mov    ax, [mcbfirst]                               ; DS = first mcb
    mov    ds, ax
mcbnext:
    mov    ax, ds                                       ; Print arena segment
    call   hexwordout
    mov    ah, 002h
    mov    dl, ' '                                      ; Print space
    int    021h
    mov    ah, 002h                                     ; Print signature
    mov    dl, [ds:memarena.signature]
    push   dx                                           ; Save signature
    int    021h
    mov    ah, 002h
    mov    dl, ' '                                      ; Print space
    int    021h
    mov    ax, [ds:memarena.owner]                      ; Print owner
    call   hexwordout
    mov    ah, 002h
    mov    dl, ' '                                      ; Print space
    int    021h
    mov    ax, [ds:memarena.size]                       ; Print size
    call   hexwordout
    mov    ah, 002h
    mov    dl, ' '                                      ; Print space
    int    021h
    xor    si, si                                       ; Print name
    mov    cx, 8
mcbnextname:
    mov    dl, [ds:si+memarena.name]                    ; Read name byte
    mov    ah, 002h                                     ; Print name byte
    int    021h
    inc    si                                           ; Increment name byte
    loop   mcbnextname                                  ; Get next name byte
    pop    dx                                           ; Restore signature
    cmp    dl, 'Z'                                      ; Compare to end arena
    jz     mcbdone                                      ;   if yes were done
    call   memarenanext                                 ; Get next arena
    jc     mcbdone                                      ;   failed were done
    call   crlf                                         ; Print CR / LF for next arena
    jmp    mcbnext                                      ; Next arena
mcbdone:
    call   crlf                                         ; Print CR / LF
    pop    ds                                           ; Restore registers
    pop    dx
    pop    ax
    ret                                                 ; Return from subroutine

;-------------------------------------------------------
; Memory Allocate Command
; Input:
;      Command line hex number
; Output:
;       None
;-------------------------------------------------------
alloc:
    push   ax                                           ; Save registers
    push   bx
    push   si
    mov    si, buffer+8                                 ; Get buffer + 8 point to hex input
    call   hexwordin                                    ; Get hex input
    mov    bx, ax                                       ; Allocate input size
    mov    ah, 048h                                     ; Service Allocate
    int    021h
    jc     allocerror
    call   crlf                                         ; Print CR / LF
    call   hexwordout                                   ; Show allocated page start
    call   crlf                                         ; Print CR / LF
allocerror:
    pop    si                                           ; Restore registers
    pop    bx
    pop    ax
    ret                                                 ; Return from subroutine

;-------------------------------------------------------
; Memory Free Command
; Input:
;      Command line hex number
; Output:
;       None
;-------------------------------------------------------
free:
    push   ax                                           ; Save registers
    push   si
    push   es
    mov    si, buffer+7                                 ; Get buffer + 8 point to hex input
    call   hexwordin                                    ; Get hex input
    mov    es, ax                                       ; Free page
    mov    ah, 049h                                     ; Service Free
    int    021h
    jc     freeerror
freeerror:
    pop    es
    pop    si                                           ; Restore registers
    pop    ax
    ret                                                 ; Return from subroutine

;-------------------------------------------------------
; Memory Resize Command
; Input:
;      Command line hex number
; Output:
;       None
;-------------------------------------------------------
resize:
    push   ax                                           ; Save registers
    push   si
    push   es
    mov    si, buffer+9                                 ; Get buffer + 8 point to hex input
    call   hexwordin                                    ; Get hex input
    mov    es, ax                                       ; Free page
    mov    si, buffer+14
    call   hexwordin                                    ; Get hex input
    mov    bx, ax
    mov    ah, 04Ah                                     ; Service Resize
    int    021h
    jc     resizeerror
resizeerror:
    pop    es
    pop    si                                           ; Restore registers
    pop    ax
    ret                                                 ; Return from subroutine


%include "io.asm"
%include "int.asm"
%include "mem.asm"

;-------------------------------------------------------
; Data Area
;-------------------------------------------------------

verstr:         db      "VER", 0
clsstr:         db      "CLS", 0
echostr:        db      "ECHO", 0
mcbstr:         db      "MCB", 0
allocstr:       db      "ALLOC", 0
freestr:        db      "FREE", 0
resizestr:      db      "RESIZE", 0

version:        db      00Dh, 00Ah, "DEMO OS V0.11"     ; Message to be printed
                db      00Dh, 00Ah, '$'

invalid:        db      00Dh, 00Ah, "Invalid Command"   ; Message to be printed
                db      00Dh, 00Ah, '$'

buffer:         db      64                              ; Buffer for string input
                db      0
                db      0, 0, 0, 0, 0, 0, 0, 0
                db      0, 0, 0, 0, 0, 0, 0, 0
                db      0, 0, 0, 0, 0, 0, 0, 0
                db      0, 0, 0, 0, 0, 0, 0, 0
                db      0, 0, 0, 0, 0, 0, 0, 0
                db      0, 0, 0, 0, 0, 0, 0, 0
                db      0, 0, 0, 0, 0, 0, 0, 0
                db      0, 0, 0, 0, 0, 0, 0, 0

mcbfirst:       dw      0
		

Modify the disk image

Now lets build our disk. If you are running the previous tutorial in VirtualBox Remember to remove the disk or close virtual box before assembling the disk image.

> nasm demoos.asm -o demoos.bin
> nasm osdisk.asm -o osdisk.img
		

Your osdisk.img can now be loaded with VirtualBox. The result should be as follows:

DEMO OS V0.11

> alloc 500

1000

> resize 1000 0600

> mcb

0FFF M 1000 600
1500 Z 0000 69FF

> 
		
Memory: Free | Index | Disk: Loader

Support This Project On Patreon

Copyright © 2021 Marcus Kelly