Question

In: Computer Science

Thinking in Assembly language What values will be written to the array when the following code...

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

Solutions

Expert Solution

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 :)


Related Solutions

The language is Assembly Language. Thank you! Assignment 3: Run the following code. Record what each...
The language is Assembly Language. Thank you! Assignment 3: Run the following code. Record what each of the flag changes are for each command. INCLUDE Irvine32.inc .data .code main PROC mov al,255 add al,1 call DumpRegs sub al,1 call DumpRegs sub al,1 call DumpRegs exit main ENDP END main
How would I code the following in assembly language? Use the Keil programming environment to code...
How would I code the following in assembly language? Use the Keil programming environment to code the C8051F330 SiLabs 8051 micro controller. All the documentation is available on efundi. The program shall be done in assembler and you shall use the DJNZ instruction to generate a delay time to flash the LED. The LED shall flash in the following sequence: 1. On for 50mS, 2. Off for 400mS, 3. On for 50mS, 4. Off for 1.5S, 5. Repeat the sequence...
Write an assembly program that rearrange the values in the following array as 40, 30, 20,...
Write an assembly program that rearrange the values in the following array as 40, 30, 20, 10: .data byte1 BYTE 10, 20, 30, 40 Save the original four byte data into eax, and the rearranged data into ebx, from the least significant bit to the most significant bit. Insert a call DumpRegs statement to display the register values.
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a,...
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a, i, and j are in registers $s0, $t0, and $t1, respectively. Assume that register $s2 holds the base address of the array A (add comments to your MIPS code). j = 0; for(i=0 ; i<a ; i++) A[i]=i+j++;
1.Note: The following code is written only in main -- you assume there is an array...
1.Note: The following code is written only in main -- you assume there is an array bag and you are not adding any methods to that bag. Write the main program to create two array bags of type string. Place 5 strings of your choice in the first bag and 5 strings (of your choice) in the second bag. Your program must: a) determine if there are any strings in the first bag that appears in the second bag as...
Exercise #1:  Write MARIE assembly language code to input 3 values into variables x, y, and z,...
Exercise #1:  Write MARIE assembly language code to input 3 values into variables x, y, and z, calculate x + y -z, and outputs the result. Run your code in the simulator and submit a screen shot of your program run and the code. //x + y -z ORG 100     INPUT     STORE X     INPUT     STORE Y     INPUT     STORE Z     LOAD X     ADD Y     SUBT Z     OUTPUT     Halt X, Dec 0 Y, DEC 0 Z, DEC 0 Exercise #2: Write MARIE assembly...
Write a sequence of assembly language instructions to subtract each entry of an array A of...
Write a sequence of assembly language instructions to subtract each entry of an array A of five two’s complement 16-bit binary integers from the corresponding entry of an array B of five two’s complement 16-bit binary integers and construct a third array C of two’s complement 16-bit binary integers. i.e. C[i] = A[i] - B[i]. Use the following data for the arrays A and B. A: 10, -15, 20, 4, -5 B: 25, -5, -30, 6, 10 please answer in...
Translate the following C code into M4K assembly language. You do not have to use the...
Translate the following C code into M4K assembly language. You do not have to use the frame pointer, just use $sp if you need to use the stack. You do not have to show the stack initialization nor stack cleanup. If you need a specific value for an address, just make an assumption. int A; main() { int B = 5; B = A+B }; // main //Disassembly starts here !main() { //stack and frame pointer init // you do...
few problems example of array and 2d array and the solution code in java language. I...
few problems example of array and 2d array and the solution code in java language. I am new to java and trying to learn this chapter and it is kinda hard for me to understand.
Write hack assembly language code for eq lt gt
Write hack assembly language code for eq lt gt
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT