Showing posts with label if statement. Show all posts
Showing posts with label if statement. Show all posts

2008/02/05

Branches (If Statements)

If without Else

if( x == 0 ) {
  x = 1;
}
x++;
Address Machine Assembly Notes
assume r0 = x
0: 9002 beq r0, 0x0004 if true, skip false
2: 8004 br 0x000a false case, skip true
4: 0000 0000 0001 ld $0x0001, r0 true case
a: 6300 inc r0 after if block

Note: the false case comes first since we branch somewhere else when true.

If with Else

if( x == 0 ) {
  x = 1;
} else {
  x = 2;
}
x++;
Address Machine Assembly Notes
assume r0 = x
0: 9005 beq r0, 0x000a if true, skip false
2: 0000 0000 0002 ld $0x0002, r0 false case
8: 8004 br 0x0010 skip true
a: 0000 0000 0001 ld $0x0001, r0 true case
10: 6300 inc r0 after if block