.text _sbrk: lw $a0, 4($sp) #POP addu $sp, $sp, 4 li $v0, 9 syscall #sbrk jr $ra .text .globl main main: sw $ra, 0($sp) #PUSH subu $sp, $sp, 4 sw $fp, 0($sp) #PUSH subu $sp, $sp, 4 addu $fp, $sp, 8 subu $sp, $fp, 12 la $t0, .L1 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 .data .L1: .asciiz "Printing 42 to the next line:\n" .text lw $a0, 4($sp) #POP addu $sp, $sp, 4 li $v0, 4 #STRING syscall li $t0, 42 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 jal _printint la $t0, .L2 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 .data .L2: .asciiz "\nCalling localtest(2), result should be \n" .text lw $a0, 4($sp) #POP addu $sp, $sp, 4 li $v0, 4 #STRING syscall # calculate address for 2nd local in any function we call # that accepts one parameter # $sp points at actual param # $sp+4 points at old $ra # $sp+8 points at old $fp # $sp+12 points at first local # $sp+16 points at second local subu $t0, $sp, 16 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 jal _printint li $t0, 2 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 jal _localtest move $t0, $v0 sw $t0, -8($fp) la $t0, .L3 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 .data .L3: .asciiz "Result is " .text lw $a0, 4($sp) #POP addu $sp, $sp, 4 li $v0, 4 #STRING syscall lw $t0, -8($fp) move $a0, $t0 li $v0, 1 #WRITE syscall la $t0, .L4 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 .data .L4: .asciiz "\n" .text lw $a0, 4($sp) #POP addu $sp, $sp, 4 li $v0, 4 #STRING syscall la $t0, .L5 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 .data .L5: .asciiz "\nCalling localtest(1), result should be \n" .text lw $a0, 4($sp) #POP addu $sp, $sp, 4 li $v0, 4 #STRING syscall # calculate address for 1st local in any function we call # that accepts one parameter # $sp points at actual param # $sp+4 points at old $ra # $sp+8 points at old $fp # $sp+12 points at first local subu $t0, $sp,12 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 jal _printint li $t0, 1 li $t0, 1 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 jal _localtest move $t0, $v0 sw $t0, -8($fp) la $t0, .L6 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 .data .L6: .asciiz "Result is " .text lw $a0, 4($sp) #POP addu $sp, $sp, 4 li $v0, 4 #STRING syscall lw $t0, -8($fp) move $a0, $t0 li $v0, 1 #WRITE syscall la $t0, .L7 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 .data .L7: .asciiz "\n" .text lw $a0, 4($sp) #POP addu $sp, $sp, 4 li $v0, 4 #STRING syscall la $t0, .L8 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 .data .L8: .asciiz "\nCalling localtest(3), result should be \n" .text lw $a0, 4($sp) #POP addu $sp, $sp, 4 li $v0, 4 #STRING syscall # calculate address for 3rd local in any function we call # that accepts one parameter # $sp points at actual param # $sp+4 points at old $ra # $sp+8 points at old $fp # $sp+12 points at first local # $sp+16 points at second local # $sp+20 points at third local subu $t0, $sp,20 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 jal _printint li $t0, 3 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 jal _localtest move $t0, $v0 sw $t0, -8($fp) la $t0, .L9 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 .data .L9: .asciiz "Result is " .text lw $a0, 4($sp) #POP addu $sp, $sp, 4 li $v0, 4 #STRING syscall lw $t0, -8($fp) move $a0, $t0 li $v0, 1 #WRITE syscall la $t0, .L10 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 .data .L10: .asciiz "\n" .text lw $a0, 4($sp) #POP addu $sp, $sp, 4 li $v0, 4 #STRING syscall la $t0, .L11 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 .data .L11: .asciiz "\nCalling localtest(4), result should be \n" .text lw $a0, 4($sp) #POP addu $sp, $sp, 4 li $v0, 4 #STRING syscall # calculate address for 4th local in any function we call # that accepts one parameter # $sp points at actual param # $sp+4 points at old $ra # $sp+8 points at old $fp # $sp+12 points at first local # $sp+16 points at second local # $sp+20 points at third local # $sp+24 points at fourth local subu $t0, $sp,24 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 jal _printint li $t0, 4 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 jal _localtest move $t0, $v0 sw $t0, -8($fp) la $t0, .L12 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 .data .L12: .asciiz "Result is " .text lw $a0, 4($sp) #POP addu $sp, $sp, 4 li $v0, 4 #STRING syscall lw $t0, -8($fp) move $a0, $t0 li $v0, 1 #WRITE syscall la $t0, .L13 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 .data .L13: .asciiz "\n" .text lw $a0, 4($sp) #POP addu $sp, $sp, 4 li $v0, 4 #STRING syscall la $t0, .L14 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 .data .L14: .asciiz "\nUsing halloc to allocate space\n" .text lw $a0, 4($sp) #POP addu $sp, $sp, 4 li $v0, 4 #STRING syscall li $t0, 324 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 jal _halloc move $t0, $v0 sw $t0, -8($fp) la $t0, .L15 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 .data .L15: .asciiz "Address to allocated memory is " .text lw $a0, 4($sp) #POP addu $sp, $sp, 4 li $v0, 4 #STRING syscall lw $t0, -8($fp) move $a0, $t0 li $v0, 1 #WRITE syscall la $t0, .L16 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 .data .L16: .asciiz "\n" .text lw $a0, 4($sp) #POP addu $sp, $sp, 4 li $v0, 4 #STRING syscall la $t0, .L17 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 .data .L17: .asciiz "\nNext allocation should be at " .text lw $a0, 4($sp) #POP addu $sp, $sp, 4 li $v0, 4 #STRING syscall lw $t0, -8($fp) sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 li $t0, 81 lw $t1, 4($sp) #POP addu $sp, $sp, 4 sll $t0, $t0, 2 add $t0, $t0, $t1 move $a0, $t0 li $v0, 1 #WRITE syscall la $t0, .L18 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 .data .L18: .asciiz "\nIt is at " .text lw $a0, 4($sp) #POP addu $sp, $sp, 4 li $v0, 4 #STRING syscall li $t0, 4 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 jal _halloc move $t0, $v0 sw $t0, -8($fp) lw $t0, -8($fp) move $a0, $t0 li $v0, 1 #WRITE syscall la $t0, .L19 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 .data .L19: .asciiz "\n" .text lw $a0, 4($sp) #POP addu $sp, $sp, 4 li $v0, 4 #STRING syscall .L0: lw $ra, 0($fp) move $t0, $fp lw $fp, -4($fp) move $sp, $t0 jr $ra