Assembly Language for Beginners

(nextflipdebug2) #1

1.14. CONDITIONAL JUMPS


.text:00000178 or $at, $zero
.text:0000017C sltu $v0, $v1, $v0
.text:00000180 beqz $v0, loc_1A8
.text:00000184 or $at, $zero
.text:00000188 lui $v0, (aAB_0 >> 16) # "a<b"
.text:0000018C addiu $a0, $v0, (aAB_0 & 0xFFFF) # "a<b"
.text:00000190 lw $v0, (puts & 0xFFFF)($gp)
.text:00000194 or $at, $zero
.text:00000198 move $t9, $v0
.text:0000019C jalr $t9
.text:000001A0 or $at, $zero
.text:000001A4 lw $gp, 0x20+var_10($fp)
.text:000001A8
.text:000001A8 loc_1A8: # CODE XREF: f_unsigned+A0
.text:000001A8 move $sp, $fp
.text:000001AC lw $ra, 0x20+var_4($sp)
.text:000001B0 lw $fp, 0x20+var_8($sp)
.text:000001B4 addiu $sp, 0x20
.text:000001B8 jr $ra
.text:000001BC or $at, $zero
.text:000001BC # End of function f_unsigned


1.14.2 Calculating absolute value.


A simple function:


int my_abs (int i)
{
if (i<0)
return -i;
else
return i;
};


Optimizing MSVC


This is how the code is usually generated:


Listing 1.118: Optimizing MSVC 2012 x64

i$ = 8
my_abs PROC
; ECX = input
test ecx, ecx
; check for sign of input value
; skip NEG instruction if sign is positive
jns SHORT $LN2@my_abs
; negate value
neg ecx
$LN2@my_abs:
; prepare result in EAX:
mov eax, ecx
ret 0
my_abs ENDP


GCC 4.9 does mostly the same.


Optimizing Keil 6/2013: Thumb mode


Listing 1.119: Optimizing Keil 6/2013: Thumb mode

my_abs PROC
CMP r0,#0
; is input value equal to zero or greater than zero?

Free download pdf