In: Computer Science
Thinking in Assembly language
What values will be written to the array when the following code executes?
.data
array DWORD 4 DUP(0)
.code
main PROC
mov eax,10
mov esi,0
call proc_1
add esi,4
add eax,10
mov array[esi],eax
INVOKE ExitProcess,0
main ENDP
proc_1 PROC
call proc_2
add esi,4
add eax,10
mov array[esi],eax
ret
proc_1 ENDP
proc_2 PROC
call proc_3
add esi,4
add eax,10
mov array[esi],eax
ret
proc_2 ENDP
proc_3 PROC
mov array[esi],eax
ret
proc_3 ENDP
Note: for simplicity purpose , instead of assigning the address in mov command, I have moved the value.
As you are well aware program execution starts from main. Let's debug step by step.
main PROC
mov eax,10 ( implies adrress of 10 is moved into eax, eax=10)
mov esi,0 (implies adrress of 0 is moved into esi , esi=0)
call proc_1 / proc_1 is called /
In proc_1 , proc_2 is called and in return which calls proc_3
proc_3 assigns address of eax to array[0] ( mov array[esi],eax) which implies array[0]=10) and we retrun to proc_2
In proc_2
add esi,4 ( add 0 and 4 and assign to esi , esi=4)
add eax,10 ( add 10 and 10 and assign to eax, eax=20)
mov array[esi],eax ( eax value 20 is moved into array[4])
ret ( return to procedure 1)
In proc_1 ( as of now we have following data, esi=4, eax=20 and array[0]=10,array[4]=20)
add esi,4 ( now esi+4=4+4=8)
add eax,10 ( eax+10=20+10=30)
mov array[esi],eax ( implies array[8]=30)
ret(return to main)
In main after call proc_1 statement we have below values, esi=8,eax=30 , array[0]=10,array[4]=20 and array[8]=30
add esi,4 ( esi+4=8+4=12)
add eax,10 (eax+10=30+10=40)
mov array[esi],esx ( 40 is assigned array to array[12])
Then by invoking exit process, we exit it and values of array is as follows array[0]=10,array[4]=20,array[8]=30 and array[12]=40
-----------------------------------------------------
Please give me a UPVOTE. Thank you :)