# a MAL program to add up the first n integers, # where n is a positive integer entered by the user. .data # strings for making the output look nice str1: .asciiz "Please enter a positive integer: " str2: .asciiz "The sum of the first " str3: .asciiz " integers is " newline: .byte '\n' # variable declarations n: .word 0 # user entered integer sum: .word 0 # running sum of the first n integers i: .word 0 # integer counter, 0 to n tmp: .word # used for comparisons .text __start: la $a0,str1 # puts str1 li $v0,4 syscall li $v0,5 # get n syscall sw $v0,n lb $a0,newline # put newline li $v0,11 syscall for: lw $a0,n # sub tmp,n,i lw $v0,i sub $v0,$a0,$v0 sw $v0,tmp lw $a0,tmp # bltz tmp,endfor bltz $a0,endfor lw $a0,sum # add sum,sum,i lw $v0,i add $v0,$a0,$v0 sw $v0,sum lw $a0,i # add i,i,1 li $v0,1 add $v0,$a0,$v0 sw $v0,i b for endfor: la $a0,str2 # puts str2 li $v0,4 syscall lw $a0,n # put n li $v0,1 syscall la $a0,str3 # puts str3 li $v0,4 syscall lw $a0,sum # put sum li $v0,1 syscall lb $a0,newline # put newline li $v0,11 syscall li $v0,10 # done syscall