Assembly Language for Beginners

(Jeff_L) #1

1.17. MORE ABOUT STRINGS


Non-optimizing GCC (Linaro) 4.9


my_strlen:
; function prologue
sub sp, sp, #32
; first argument (str) will be stored in [sp,8]
str x0, [sp,8]
ldr x0, [sp,8]
; copy "str" to "eos" variable
str x0, [sp,24]
nop
.L62:
; eos++
ldr x0, [sp,24] ; load "eos" to X0
add x1, x0, 1 ; increment X0
str x1, [sp,24] ; save X0 to "eos"
; load byte from memory at address in X0 to W0
ldrb w0, [x0]
; is it zero? (WZR is the 32-bit register always contain zero)
cmp w0, wzr
; jump if not zero (Branch Not Equal)
bne .L62
; zero byte found. now calculate difference.
; load "eos" to X1
ldr x1, [sp,24]
; load "str" to X0
ldr x0, [sp,8]
; calculate difference
sub x0, x1, x0
; decrement result
sub w0, w0, #1
; function epilogue
add sp, sp, 32
ret


It’s more verbose. The variables are often tossed here to and from memory (local stack). The same
mistake here: the decrement operation happens on a 32-bit register part.


MIPS


Listing 1.187: Optimizing GCC 4.4.5 (IDA)

my_strlen:
; "eos" variable will always reside in $v1:
move $v1, $a0


loc_4:
; load byte at address in "eos" into $a1:
lb $a1, 0($v1)
or $at, $zero ; load delay slot, NOP
; if loaded byte is not zero, jump to loc_4:
bnez $a1, loc_4
; increment "eos" anyway:
addiu $v1, 1 ; branch delay slot
; loop finished. invert "str" variable:
nor $v0, $zero, $a0
; $v0=-str-1
jr $ra
; return value = $v1 + $v0 = eos + ( -str-1 ) = eos - str - 1
addu $v0, $v1, $v0 ; branch delay slot


MIPS lacks aNOTinstruction, but hasNORwhich isOR + NOToperation.


This operation is widely used in digital electronics^107. For example, the Apollo Guidance Computer used
in the Apollo program, was built by only using 5600 NOR gates: [Jens Eickhoff,Onboard Computers,


(^107) NOR is called “universal gate”

Free download pdf