assembly - If register is equal to 0 then -


trying make simple if statement school assignment.

.data origin:     .asciiz "x , y @ origin!!!" # string statment origin  inputx:     .asciiz "what x coordinate?"  test:     .asciiz "this test" .text   main:  la $a0, inputx #puts inputx string address 0 li $v0, 4 # prints string @ address 0 syscall  li $v0 , 5 # reads input integer syscall   move $s0, $v0# moves value input integer $s0 (x value)    beqz $s0, endif # should if input entered 0 go endif label    li $v0, 10 syscall  endif:  la $a0, test #print out our test string li $v0, 4 syscall 

from understand. beqz checks if $s0 zero. should go label.

this part confuses me. label go? how decide when not in label , want go original code?

basically if input x 0 want print out "test" string

branches in assembly not subroutines. once branch line number, you're @ line number , continue increasing program counter there. so, once beqz endif:, you're @ endif, you'll print string , run off uninitialized memory land. there's no mechanism return.

consider following:

.data if_string: .asciiz "if\n" else_string: .asciiz "else\n" end_string: .asciiz "end\n"  .text main:   li $s0,2   bnez $s0,else    la $a0,if_string   li $v0,4   syscall    b end  else:   la $a0,else_string   li $v0,4   syscall    #b end implicit here -- fall straight through  end:   la $a0,end_string   li $v0,4   syscall    li $v0,10   syscall 

if set $s0 0, you'll go through "if" portion of code -- if isn't 0 jump "else" portion of code. there's no returning, next part of program can branch directly end label. in else portion, there's no need branch branch next line (and waste bunch of time doing so).


Comments