Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

simpleAssembler should return a -1 if an error has occurred and 0 otherwise. Mak

ID: 3841414 • Letter: S

Question

simpleAssembler should return a -1 if an error has occurred and 0 otherwise.

Make sure to post the two screenshot showing above situations "without screenshot not accepted"

Simple Assembly Interpreter

Develop a simple assembly code interpreter for a theoretical processor with the following properties.

This processor only has seven instructions:

You can assume this simple processor has only one register and 256 32-bit memory locations. Your simple assembler will given an array containing instructions and will convert the instructions into x86 code on the fly. You should be able to do this through a combination of C and inline assembly. The only parts that have to be in assembly are the code elements for the above instructions. For instance, a lodinstruction could become a mov eax instruction.

The mem32 addresses are in terms of bytes. For instance, an address of 0x10 would refer to the 17th byte, or the 5th integer (0 based index).

Immediate values are hex numbers without a 0x in front. Memory address are hex numbers with the 0x in front.

As noted, your project can use a combination of C and assembly. You can use C string processing functions from the C standard library.

Your function should look like the following:

linesOfCode is an array of strings containing the code lines you are to execute. memory is a 256 element 32-bit integer array that is given to you. lineCount is the count of the number of lines in the linesOfCodearray.

simpleAssembler should return a -1 if an error has occurred and 0 otherwise.

Explanation / Answer

.LC0:
.string "%c"
simpleAssembler(char** linesOfCode, int lineCount, int* memory):
push rbp
mov rbp, rsp
sub rsp, 48
mov QWORD PTR [rbp-24], rdi
mov DWORD PTR [rbp-28], esi
mov QWORD PTR [rbp-40], rdx
mov DWORD PTR [rbp-4], 0
.L6:
mov eax, DWORD PTR [rbp-4]
cmp eax, DWORD PTR [rbp-28]
jge .L2
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*8]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov rax, QWORD PTR [rax]
test rax, rax
je .L3
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*8]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov rax, QWORD PTR [rax]
mov rsi, rax
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call printf
add QWORD PTR [rbp-40], 4
jmp .L7
.L3:
mov eax, -1
jmp .L5
.L7:
add DWORD PTR [rbp-4], 1
jmp .L6
.L2:
mov eax, 0
.L5:
leave
ret