# This program includes a simple example of a procedure call .data # The following entries go in the data segment strName: .asciiz "Author: Doug Johnson\n" strComma: .asciiz "," strSeparator: .asciiz " -> " strNewline: .asciiz "\n" coords: # must have an even number of integer elements .word 0,1,2,3,4,5,6,7,8,9,0xa,0xb,0xc,0xd,0xe,0xf endtag: # must be the address after the array # main is called with # $a0 arg count (argc) # $a1 arg string array address (argv) # $a2 environment string array address (envp) # main stack frame layout # 28($sp) unused # 24($sp) $ra # 20($sp) $s1 # 16($sp) $s0 # 12($sp) reserved for callee: $a3 save space # 8($sp) reserved for callee: $a2 save space # 4($sp) reserved for callee: $a1 save space # 0($sp) reserved for callee: $a0 save space .text # The following entries go in the text (program code) segment main: subu $sp,$sp,32 # create stack frame sw $ra,24($sp) # save return address sw $s1,20($sp) # save $s1 sw $s0,16($sp) # save $s0 # look at each pair of entries in the array # print the vector, add delta to the vector, print the result vector la $s0,coords # $s0 = next = address(coords) la $s1,endtag # $s1 = endtag = address(endtag) beq $s0,$s1,mainExit # if array length is zero, then quit mainLoopA: move $a0,$s0 # address of next pair jal v2Print # print the vector la $a0,strSeparator # separator string address li $v0,4 # print_string code syscall # print move $a0,$s0 # reload pair address li $a1,0 # load address of offset amount li $a2,1 # load address of resulting vector jal swap # swap the elements move $a0,$s0 # reload pair address jal v2Print # print la $a0,strNewline # newline address li $v0,4 # print_string code syscall # print addi $s0,8 # point to next vector in array bne $s0,$s1,mainLoopA # loop if more data in array mainExit: lw $ra,24($sp) # restore return address lw $s1,20($sp) # restore $s1 lw $s0,16($sp) # restore $s0 addu $sp,$sp,32 # release stack frame jr $ra # return #-------------------------------------------------------------------- # swap(int *vec, int i, int j) # Swap vec[i] and vec[j] # $a0 - vec - address of the int array # $a1 - i - index of first value to swap # $a2 - j - index of second value to swap swap: sll $a1,$a1,2 # $a1 = 4*i addu $a1,$a1,$a0 # $a1 = addr(a[i]) lw $t1,0($a1) # $t1 = a[i] sll $a2,$a2,2 # $a2 = 4*j addu $a2,$a2,$a0 # $a2 = addr(a[j]) lw $t0,0($a2) # $t0 = a[j] sw $t0,0($a1) # a[i] = old a[j] sw $t1,0($a2) # a[j] = old a[i] jr $ra # return #-------------------------------------------------------------------- # v2Print(int *vec) # Print the contents of a vector in memory # $a0 - vec - address of the 2-element word vector to print v2Print: move $t0,$a0 # remember address of vec lw $a0,0($t0) # load the x coordinate li $v0,1 # load print_integer code syscall # print la $a0,strComma # load address of the string to print li $v0,4 # load print_string code syscall # print lw $a0,4($t0) # load the y coordinate li $v0,1 # load print_integer code syscall # print jr $ra