Assembly Language for Beginners

(Jeff_L) #1

1.20. ARRAYS


and esp, 0FFFFFFF0h
sub esp, 70h
mov [esp+70h+i], 0 ; i=0
jmp short loc_804840A

loc_80483F7:
mov eax, [esp+70h+i]
mov edx, [esp+70h+i]
add edx, edx ; edx=i2
mov [esp+eax
4+70h+i_2], edx
add [esp+70h+i], 1 ; i++


loc_804840A:
cmp [esp+70h+i], 13h
jle short loc_80483F7
mov [esp+70h+i], 0
jmp short loc_8048441


loc_804841B:
mov eax, [esp+70h+i]
mov edx, [esp+eax*4+70h+i_2]
mov eax, offset aADD ; "a[%d]=%d\n"
mov [esp+70h+var_68], edx
mov edx, [esp+70h+i]
mov [esp+70h+var_6C], edx
mov [esp+70h+var_70], eax
call _printf
add [esp+70h+i], 1


loc_8048441:
cmp [esp+70h+i], 13h
jle short loc_804841B
mov eax, 0
leave
retn
main endp


By the way, variableais of typeint*(the pointer toint)—you can pass a pointer to an array to another
function,butit’smorecorrecttosaythatapointertothefirstelementofthearrayispassed(theaddresses
of rest of the elements are calculated in an obvious way).


If you index this pointer asa[idx],idxis just to be added to the pointer and the element placed there (to
which calculated pointer is pointing) is to be returned.


An interesting example: a string of characters like“string”is an array of characters and it has a type of
const char[].


An index can also be applied to this pointer.


And that is why it is possible to write things like“string”[i]—this is a correct C/C++ expression!


ARM


Non-optimizing Keil 6/2013 (ARM mode)


EXPORT _main
_main
STMFD SP!, {R4,LR}
SUB SP, SP, #0x50 ; allocate place for 20 int variables


; first loop


MOV R4, #0 ; i
B loc_4A0
loc_494
MOV R0, R4,LSL#1 ; R0=R42
STR R0, [SP,R4,LSL#2] ; store R0 to SP+R4<<2 (same as SP+R4
4)
ADD R4, R4, #1 ; i=i+1

Free download pdf