Need help understanding mips mars code Photo for reference Why and when do we kn
ID: 3754492 • Letter: N
Question
Need help understanding mips mars code
Photo for reference
Why and when do we know when to use $s over $t?
also on line 15 and 19 there is a z in the end of beq and ble, why is that?
1 data 2 3 prompt: .asciiz " Give me your zip code (0 to stop) : " #prompt for st prompt2: .asciiz "nThe sum of all digit in your zip code is " 5 text 6 7 loop la $a0, prompt #prompt the user for zip-code syscall li $v0, 5 syscall #user enters zip-code move $tl,$vo beaz $t1, finish #if zero then jump to finish li $s0,0 #loa 0 d suum to innerLOOp.: blez $tl,printSum div $s1, sti, 10 #divide by 10 nul $s1,$s1, 10 #multiply result by 10 sub $s3, $t1,$s1 #get the reminder when divide by 10 add $s0,$s0, $s3 #add reminder to s0 that store sum of digit div $t1 ,$t1 , 10 #set ti to result of divide by 10 j innerLoop 21 3Explanation / Answer
$s and$t- Both are temporary registers,but difference is how they are used.$t variables are temporary caller saver register while $s variables are callee saved registers. It means $s will retain their value after returing from a called funtion but $t don't. For example if a function A uses register $t0 and $s0, and then call a function B, it must save the register $t0 if it wants to use it after function B returns. And functionB must save $s0 before it begins using it.
beqz means branch if register is equal to zero.
blez means branch if register is less than or equals to zero.
The above code declare two variables prompt and prompt2.
li $v0,4-immediately load 4 to register $v0
la $a0,prompt-prompt the user to enter a value and store in register $a0
syscall generates a software interupt
li $v0,5 means $v0=5
move $t1,$v0-means $t1=$v0
beqz $t1,finish-It test if value at register $t1 is 0 or not.If 0 then program ends.
li $s0,0 -means $s0=0
blez $t1,printsum- test if value in register $t1 is less than or equals to zero.if yes goto label printsum else following statements are executed
div $s1,St1,10--$s1=$t1/10
mul $s1,$s1*10--$s1=$s1*10
sub $s3,$t1,$s1--$s3=$t1-$s1
add $s0,$s0,$s3--$s0=$s0+$s3
div $t1,$t1,10--$t1=$t1/10
j interloop means its the end of else condition.and control go back to label innerloop where it again test a condition by blez$t1,printsum.