Embed presentation
Download to read offline
![Please convert the following C code to assembly Y86
int j,k;
.....
for (int i=0; i <5; i++) {
j = i*2;
k = j+1;
}
Solution
Answer:
MIPS Assembly Langauge Code :
.zero 1
main:
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-4], 0
.L3:
cmp DWORD PTR [rbp-4], 4
jg .L2
mov eax, DWORD PTR [rbp-4]
add eax, eax
mov DWORD PTR [rbp-8], eax
mov eax, DWORD PTR [rbp-8]
add eax, 1
mov DWORD PTR [rbp-12], eax
add DWORD PTR [rbp-4], 1
jmp .L3
.L2:
mov eax, 0
pop rbp
ret
__static_initialization_and_destruction_0(int, int):
push rbp
mov rbp, rsp](https://image.slidesharecdn.com/pleaseconvertthefollowingccodetoassemblyy86intjk-230707182626-29733ecc/75/Please-convert-the-following-C-code-to-assembly-Y86int-j-k-pdf-1-2048.jpg)
![sub rsp, 16
mov DWORD PTR [rbp-4], edi
mov DWORD PTR [rbp-8], esi
cmp DWORD PTR [rbp-4], 1
jne .L7
cmp DWORD PTR [rbp-8], 65535
jne .L7
mov edi, OFFSET FLAT:std::__ioinit
call std::ios_base::Init::Init()
mov edx, OFFSET FLAT:__dso_handle
mov esi, OFFSET FLAT:std::__ioinit
mov edi, OFFSET FLAT:std::ios_base::Init::~Init()
call __cxa_atexit
.L7:
nop
leave
ret
push rbp
mov rbp, rsp
mov esi, 65535
mov edi, 1
call __static_initialization_and_destruction_0(int, int)
pop rbp
ret](https://image.slidesharecdn.com/pleaseconvertthefollowingccodetoassemblyy86intjk-230707182626-29733ecc/75/Please-convert-the-following-C-code-to-assembly-Y86int-j-k-pdf-2-2048.jpg)
This document contains a request to convert C code to Y86 assembly language. It provides the assembly code solution in MIPS format, illustrating a loop operation and static initialization and destruction. The assembly code details the manipulation of local variables and calls to system functions.
![Please convert the following C code to assembly Y86
int j,k;
.....
for (int i=0; i <5; i++) {
j = i*2;
k = j+1;
}
Solution
Answer:
MIPS Assembly Langauge Code :
.zero 1
main:
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-4], 0
.L3:
cmp DWORD PTR [rbp-4], 4
jg .L2
mov eax, DWORD PTR [rbp-4]
add eax, eax
mov DWORD PTR [rbp-8], eax
mov eax, DWORD PTR [rbp-8]
add eax, 1
mov DWORD PTR [rbp-12], eax
add DWORD PTR [rbp-4], 1
jmp .L3
.L2:
mov eax, 0
pop rbp
ret
__static_initialization_and_destruction_0(int, int):
push rbp
mov rbp, rsp](https://image.slidesharecdn.com/pleaseconvertthefollowingccodetoassemblyy86intjk-230707182626-29733ecc/75/Please-convert-the-following-C-code-to-assembly-Y86int-j-k-pdf-1-2048.jpg)
![sub rsp, 16
mov DWORD PTR [rbp-4], edi
mov DWORD PTR [rbp-8], esi
cmp DWORD PTR [rbp-4], 1
jne .L7
cmp DWORD PTR [rbp-8], 65535
jne .L7
mov edi, OFFSET FLAT:std::__ioinit
call std::ios_base::Init::Init()
mov edx, OFFSET FLAT:__dso_handle
mov esi, OFFSET FLAT:std::__ioinit
mov edi, OFFSET FLAT:std::ios_base::Init::~Init()
call __cxa_atexit
.L7:
nop
leave
ret
push rbp
mov rbp, rsp
mov esi, 65535
mov edi, 1
call __static_initialization_and_destruction_0(int, int)
pop rbp
ret](https://image.slidesharecdn.com/pleaseconvertthefollowingccodetoassemblyy86intjk-230707182626-29733ecc/75/Please-convert-the-following-C-code-to-assembly-Y86int-j-k-pdf-2-2048.jpg)