*If you have trouble viewing instructions/images below, please refer to the \"As
ID: 3859656 • Letter: #
Question
*If you have trouble viewing instructions/images below, please refer to the "Assignment" chapter in Unit 6's Learning Guide
For the Unit 6 assignment, hand compile the following two assembly language programs into machine language that can be loaded into an executed on the Hack computer.
Chapter 4 of the Nisan and Schocken text describes the machine language instructions that are supported by the Hack computer system. It is important to understand that different computer architectures will support different capabilities and of course this translates into differences in both the machine language instructions of the computer and the assembler language that supports the system.
In the Hack computer platform, there are essentially two types of machine language instructions the A instruction (see below)
And the C instruction as follows. ALL Hack computer programs can be developed using these two instructions.
The C instruction allows the programmer to interact with the CPU(and in particular the ALU) to perform computations against the values in the registers. If you recall, in Unit 2 we presented the ALU for the Hack computer as follows:
Each of the instructions that the Hack ALU is capable of running can be specified to the ALU through a set of control bits. These control bits or the C bits in the instruction and their associated functions are listed below.
IT IS IMPORTANT TO COMPLETE THIS EXERCISE. I caution you not to take any shortcuts. The best way to really gain an understanding of how machine language and assembler language works is by learning how to convert assembler into machine language. We will gain an understanding of the relationship between hardware and software in this process. As such I urge you to NOT be tempted to cheat and use a compiler for this task.
Once you have been able to translate each of the following two programs written in Hack Assembler into binary machine code, Load your machine language program into the cpu simulator and execute it.
For your assignment post a description of YOUR process to convert the assembler symbols into machine language and what you learned in the process. Also include both your assembler program and corresponding machine language program in your assignment.
//Program 1 to convert to machine language
// Computes R0 = 2 + 3
@2
D=A
@3
D=D+A
@0
M=D
// Program 2 to convert to machine language
// Symbol-less version of the Max.asm program.
@0
D=M
@1
D=D-M
@10
D;JGT
@1
D=M
@12
0;JMP
@0
D=M
@2
M=D
@14
0;JMP
Explanation / Answer
Answer:
.LC0:
.string "Enter operator either + or - or * or /: "
.LC1:
.string "Enter two operands: "
.LC2:
.string "Error! operator is not correct"
main:
push rbp
mov rbp, rsp
sub rsp, 16
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-1]
mov rsi, rax
mov edi, OFFSET FLAT:std::cin
call std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char&)
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*)
lea rax, [rbp-8]
mov rsi, rax
mov edi, OFFSET FLAT:std::cin
call std::basic_istream<char, std::char_traits<char> >::operator>>(float&)
mov rdx, rax
lea rax, [rbp-12]
mov rsi, rax
mov rdi, rdx
call std::basic_istream<char, std::char_traits<char> >::operator>>(float&)
movzx eax, BYTE PTR [rbp-1]
movsx eax, al
cmp eax, 43
je .L3
cmp eax, 43
jg .L4
cmp eax, 42
je .L5
jmp .L2
.L4:
cmp eax, 45
je .L6
cmp eax, 47
je .L7
jmp .L2
.L3:
movss xmm1, DWORD PTR [rbp-8]
movss xmm0, DWORD PTR [rbp-12]
addss xmm0, xmm1
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(float)
jmp .L8
.L6:
movss xmm0, DWORD PTR [rbp-8]
movss xmm1, DWORD PTR [rbp-12]
subss xmm0, xmm1
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(float)
jmp .L8
.L5:
movss xmm1, DWORD PTR [rbp-8]
movss xmm0, DWORD PTR [rbp-12]
mulss xmm0, xmm1
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(float)
jmp .L8
.L7:
movss xmm0, DWORD PTR [rbp-8]
movss xmm1, DWORD PTR [rbp-12]
divss xmm0, xmm1
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(float)
jmp .L8
.L2:
mov esi, OFFSET FLAT:.LC2
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*)
nop
.L8:
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 .L12
cmp DWORD PTR [rbp-8], 65535
jne .L12
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
.L12:
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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.