Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
program to compute the sum for an integer from 1 to n ,the user should keep enter integers until enter 0

# i..>$t0   sum...>$t1   n...>$t2


.data
prompt1:    .asciiz "Enter a number"
prompt2:    .asciiz "The sum of entered number is: "

.text

main:
li $v0, 4             #System call code for print String
la $a0, prompt1       #load address of prompt1
syscall               #Call OS to print prompt1


li $v0, 5             #System call code for read integer
syscall               #Call OS to read integer into $v0
move $t2, $v0         #move the number to read into $t2

addi $t0, $zero,1     #i=0+1
add $t1, $zero,$zero  #sum=0+0


loop:   slt $t3, $t2,$t0            #t4= if n(i)
        bnq $zero, $t3, goo       #if 0==t4 go to "goo"
	  
        add $t1, $t1, $t0           #sum=+1
        addi $t0, $t0, 1            # i+=1
	 
	j loop
	  		
goo:   beq $t2, $zero, exit     #if n==0 go to "go"
       li $v0, 4
       la $a0, prompt2
       syscall               # print out "Sum = "

       li $v0, 1
       move $a0, $t1
       syscall               # print out actual sum

	    
 exit:
       li $v0, 10
       syscall
Posted
Updated 22-Apr-15 5:57am
v2
Comments
CHill60 22-Apr-15 11:57am    
What is the problem?
[no name] 22-Apr-15 12:15pm    
i do not know but it Does not work
[no name] 22-Apr-15 12:28pm    
this is error but i do not know why
bnq $zero, $t3, goo
Richard MacCutchan 22-Apr-15 12:50pm    
Are you sure the mnemonic is bnq? My assembler uses bne.
[no name] 22-Apr-15 12:56pm    
yes that is true, sorry i did not pay attention
so, just this is error in this code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900