; ; HW #9 ; ; ; hw9-1.asm .ORIG x4000 INIT ADD R6, R6, #-1 ; create space for return value STR R7, R6, #-1 ; save return address STR R5, R6, #-2 ; save caller's frame pointer ADD R6, R6, #-2 ; Move stack pointer ADD R5, R6, #-1 ; Set frame pointer STR R1, R6, #-1 ; store R1 STR R2, R6, #-2 ; store R2 ADD R6, R6, #-2 ; Move stack pointer LEA R1, FREE_LIST ; get address of FREE list LD R2, HEAP_START ; get heap start address STR R2, R1, #0 ; store heap start into free list LD R1, HEAP_SIZE STR R1, R2, #0 ; store heap size as first element AND R1, R1, #0 STR R1, R2, #1 ; store null pointer as second element LDR R2, R6, #0 ; restore R2 LDR R1, R6, #1 ; restore R1 LDR R5, R6, #2 ; restore caller's frame pointer LDR R7, R6, #3 ; restore return address ADD R6, R6, #4 ; stack pointer points to return value RET ; Note that you can use the .BLKWTO assembler directive to set the ; address of where NEW is to start ; syntax: .BLKWTO NEW ADD R6, R6, #-1 ; create space for return value STR R7, R6, #-1 ; save return address STR R5, R6, #-2 ; save caller's frame pointer ADD R6, R6, #-2 ; move stack pointer ADD R5, R6, #-1 ; set frame pointer ; Put contents of new in here LDR R5, R6, #0 ; restore caller's frame pointer LDR R7, R6, #1 ; restore return address ADD R6, R6, #2 ; stack pointer points to return value RET ; You can use the .BLKWTO also for FREE FREE ADD R6, R6, #-1 ; create space for return value STR R7, R6, #-1 ; save return address STR R5, R6, #-2 ; save caller's frame pointer ADD R6, R6, #-2 ; move stack pointer ADD R5, R6, #-1 ; set frame pointer ; Put contents of free in here LDR R5, R6, #0 ; restore caller's frame pointer LDR R7, R6, #1 ; restore return address ADD R6, R6, #2 ; stack pointer points to return value RET FREE_LIST .FILL x0000 HEAP_START .FILL x9000 HEAP_SIZE .FILL x3000 .END