.text .globl main main: #ask for a number addi $v0, $zero, 51 #load the syscall id into v0 la $a0, msg #la - load the address of the string syscall add $s0, $a0, $zero #copy the return value int s0 #ask for a number addi $v0, $zero, 51 la $a0, msg syscall add $s1, $a0, $zero #copy the return value int s1 #call the sum functiona1 add $a0, $s0, $zero #set up the first parameter add $a1, $s1, $zero #set up the second parameter jal sum #call the method add $s2, $v0, $zero #copy the return value into a variable #print the result addi $v0, $zero, 56 la $a0, msg2 #prep the string argument add $a1, $s2, $zero #prep the int argument syscall #call the MessageDialogInt system call #exit addi $v0, $zero, 10 syscall #define a function to all the numbers between the two parameters sum: #allocate memory on the stack for the variable we use. #save the saved registers we will use: fp, ra, s0, s1, s2, s3 addi $sp, $sp, -24 sw $fp, 20($sp) sw $ra, 16($sp) sw $s0, 12($sp) sw $s1, 8($sp) sw $s2, 4($sp) sw $s3, 0($sp) #modify the frame pointer addi $fp, $sp, 20 #make s0 the smaller of a0 and a1 # if(a0 < a1){ # s0 = a0; # s1 = a1; # } # else { # s0 = a1; # s1 = a0; # } #make s1 the larger slt $t0, $a0, $a1 #set t0 to 1 if a0 < a1 beq $t0, $zero, else add $s0, $zero, $a0 add $s1, $zero, $a1 j endif else: add $s0, $zero, $a1 add $s1, $zero, $a0 endif: #perform the calculation (loop from one to the other) #for(s2 = s0; s2 <= s1; s2++) # s3 = s3 + s2; add $s2, $zero, $s0 add $s3, $zero, $zero #s3 = 0 comp: addi $t1, $s1, 1 slt $t0, $s2, $t1 #check if s2 <= s1 beq $t0, $zero end #if not fall out of the loop add $s3, $s3, $s2 addi $s2, $s2, 1 j comp end: #put the return value in v0 add $v0, $s3, $zero #restore the saved registers lw $fp, 20($sp) lw $ra, 16($sp) lw $s0, 12($sp) lw $s1, 8($sp) lw $s2, 4($sp) lw $s3, 0($sp) #pop the stack frame addi $sp, $sp, -24 #return to the caller's address jr $ra .data .align 0 msg: .asciiz "Enter a number." msg2: .asciiz "The sum is: "