In this program, you are to simulate a 2-pass assembler in C++. The input to thi
ID: 3823301 • Letter: I
Question
In this program, you are to simulate a 2-pass assembler in C++. The input to this program is an assembly language program and the output should include a symbol table and the machine code version of the input. You have to echo the input. Do file I/O.
You are implementing two classical algorithms (Fig. 6.1 & Fig 6.2).
INPUT
ORG $00000500
MOVE #79, $00002000
TRAP #1 ;READLN (Y), INPUT INTO DO
LOOP1 MOVE DO, $00020004 ;STORE DO IN LOCATION Y
MOVE $00002000, DO ;FETCH VARIABLE I FOR THE AD
ADD $00002004, DO ;ADD Y+1
MOVE DO, $00002002 ; STORE THE SUM IN X
LOOP2 ADDI #18, $00002002 ;ADD 18 TO X
TRAP #2 ; PRINT X
TRAP #0 ; STOP
END
OUTPUT
Address Machine code Operands Instructions
00000500 33FC 4F MOVE #79, $00002000
----- ----- -- -- -- --
----- ----- -- -- -- --
Explanation / Answer
Answer:
Assembly Language Code :
.LC0:
.string "Enter a positive integer: "
.LC1:
.string "Sum = "
main:
push rbp
mov rbp, rsp
sub rsp, 16
mov DWORD PTR [rbp-4], 0
mov esi, OFFSET FLAT:.LC0
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
lea rax, [rbp-12]
mov rsi, rax
mov edi, OFFSET FLAT:std::cin
call std::basic_istream<char, std::char_traits<char> >::operator>>(int&)
mov DWORD PTR [rbp-8], 1
.L3:
mov eax, DWORD PTR [rbp-12]
cmp DWORD PTR [rbp-8], eax
jg .L2
mov eax, DWORD PTR [rbp-8]
add DWORD PTR [rbp-4], eax
add DWORD PTR [rbp-8], 1
jmp .L3
.L2:
mov esi, OFFSET FLAT:.LC1
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov rdx, rax
mov eax, DWORD PTR [rbp-4]
mov esi, eax
mov rdi, rdx
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov eax, 0
leave
ret
__static_initialization_and_destruction_0(int, int):
push rbp
mov rbp, rsp
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
_GLOBAL__sub_I_main:
push rbp
mov rbp, rsp
mov esi, 65535
mov edi, 1
call __static_initialization_and_destruction_0(int, int)
pop rbp
ret
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.