I need to implement the following while loop in assembly:
i = 1;
while (i <= 50) {
A[i] = i;
i++;
}
With the array of integers A stored at memory location x+200, where x is the address of the memory location where the assembly program is loaded.
I can only use following commands; LOAD, STORE, ARITHMETIC (ADD, SUB, MUL, DIV, INC), SKIP, BRANCHING (BLT, BGT, BLEQ, BGEQ, BEQ), READ and WRITE, and HALT
Code in C++:-
int i = 1;
int A[51];
while (i <= 50) {
A[i] = i;
i++;
}
Code in Assembaly:-
#Refer Comments for better understanding:-
push rbp
mov rbp, rsp #Setting up stack pointers and base poiniters
sub rsp, 96
mov DWORD PTR [rbp-212], edi #Loading array base address
mov DWORD PTR [rbp-4], 1 #Initialisation of variable i.
.L3:
cmp DWORD PTR [rbp-4], 50 # Condition inside while loop.
jg .L4
mov eax, DWORD PTR [rbp-4]
cdqe
mov edx, DWORD PTR [rbp-4] #Loading variable i into register edx.
mov DWORD PTR [rbp-208+rax*4], edx #Assignment at array location A[i]=i.
add DWORD PTR [rbp-4], 1 #Increment of variable i.
jmp .L3 #continue with next iteration.
.L4:
nop
leave
ret
Get Answers For Free
Most questions answered within 1 hours.