Assembly Language for Beginners

(nextflipdebug2) #1

1.20. ARRAYS


; load address of the "a[%d]=%d\n" string:
adrp x0, .LC0
add x0, x0, :lo12:.LC0
; load "i" variable to W1 and pass it to printf() as second argument:
ldr w1, [x29,108]
; W2 still contains the value of array element which was just loaded.
; call printf():
bl printf
; increment "i" variable:
ldr w0, [x29,108]
add w0, w0, 1
str w0, [x29,108]
.L4:
; are we finished?
ldr w0, [x29,108]
cmp w0, 19
; jump to the loop body begin if not:
ble .L5
; return 0
mov w0, 0
; restore FP and LR:
ldp x29, x30, [sp], 112
ret


MIPS


The function uses a lot of S- registers which must be preserved, so that’s why its values are saved in the
function prologue and restored in the epilogue.


Listing 1.227: Optimizing GCC 4.4.5 (IDA)

main:


var_70 = -0x70
var_68 = -0x68
var_14 = -0x14
var_10 = -0x10
var_C = -0xC
var_8 = -8
var_4 = -4
; function prologue:
lui $gp, (gnu_local_gp >> 16)
addiu $sp, -0x80
la $gp, (
gnu_local_gp & 0xFFFF)
sw $ra, 0x80+var_4($sp)
sw $s3, 0x80+var_8($sp)
sw $s2, 0x80+var_C($sp)
sw $s1, 0x80+var_10($sp)
sw $s0, 0x80+var_14($sp)
sw $gp, 0x80+var_70($sp)
addiu $s1, $sp, 0x80+var_68
move $v1, $s1
move $v0, $zero
; that value will be used as a loop terminator.
; it was precalculated by GCC compiler at compile stage:
li $a0, 0x28 # '('


loc_34: # CODE XREF: main+3C
; store value into memory:
sw $v0, 0($v1)
; increase value to be stored by 2 at each iteration:
addiu $v0, 2
; loop terminator reached?
bne $v0, $a0, loc_34
; add 4 to address anyway:
addiu $v1, 4
; array filling loop is ended
; second loop begin

Free download pdf