0% found this document useful (0 votes)
206 views9 pages

Assignment PDF

The document describes the Hack CPU and its A and C instructions. It provides: 1) A description of the A-instructions, which load the A register with a constant between 0-32767 without accessing RAM. 2) A description of the C-instructions, which include computation codes, destination registers, and optional jump directives. 3) An example conversion of Program 1 from assembly to machine code to demonstrate how constants are converted to binary and instructions are translated based on their operation and reference diagrams.

Uploaded by

Morassa Chona
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
206 views9 pages

Assignment PDF

The document describes the Hack CPU and its A and C instructions. It provides: 1) A description of the A-instructions, which load the A register with a constant between 0-32767 without accessing RAM. 2) A description of the C-instructions, which include computation codes, destination registers, and optional jump directives. 3) An example conversion of Program 1 from assembly to machine code to demonstrate how constants are converted to binary and instructions are translated based on their operation and reference diagrams.

Uploaded by

Morassa Chona
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Unit 6 Assignment

Hack CPU description: it has A and D registers and pseudo-register M which refer to transferring data
between CPU and RAM.

Hack CPU has two types of instructions A-instructions and C-instructions. These two instructions are
going to help us solve the assignment, and here is how we are going to use these instructions.

1. A-instructions description

- A-instructions load A register with a constant value between 0 and 32767 and they don’t
read from RAM.

- Assembly language syntax is as follows

a. @value – instructions will load a non-negative decimal constant into register A.

b. @symbol – instructions will load the address of the code label or RAM variable symbol
into the A register.

NOTE: The A-instruction is used for three different purposes. First, it provides the only way to enter a
constant into the computer under program control. Second, it sets the stage for a subsequent
C-instruction designed to manipulate a certain data memory location, by first setting A to the address of
that location. Third, it sets the stage for a subsequent C-instruction that specifies a jump, by first loading
the address of the jump destination to the A register.
Predefined Symbols

A: Address Register.

D: Data Register.

M: Refers to the register in Main Memory whose address is currently stored in A.

SP: RAM address 0.

LCL: RAM address 1.

ARG: RAM address 2.

THIS: RAM address 3.

THAT: RAM address 4.

R0-R15: Addresses of 16 RAM Registers, mapped from 0 to 15.

SCREEN: Base address of the Screen Map in Main Memory, which is equal to 16384.

KBD: Keyboard Register address in Main Memory, which is equal to 24576.


2. C-instructions description

Assembly language syntax: dest = comp; jmp, where:

1. Dest: destination register in which the result of computation will be stored.

2. Comp: computation code

3. Jmp: the jump directive.

Binary syntax:

1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3, where:

- 111 bits: C-Instructions always begin with bits 111.

- a bit: Chooses to load the contents of either A register or M (Main Memory register
addressed by A) into the ALU for computation.

- Bits c1 through c6: Control bits expected by the ALU to perform arithmetic or bit-wise logic
operations.

- Bits d1 through d3: Specify which memory location to store the result of ALU computation
into: A, D or M.

- Bits j1 through j3: Specify which JUMP directive to execute (either conditional or
unconditional).

After describing the A and C-instructions am using the reference image to be able to convert program 1
and 2 to machine language.
Below is the reference image from
nandetetrics.

Reference IMAGE for C-instruction

Converting program 1 from assembly to machine code.

//Program 1 to convert to machine language


// Computes R0 = 2 + 3

@2
D=A
@3
D=D+A
@0
M=D

Assembly language Machine Language


@2 0000000000000010
D=A 1110110000010000
@3 0000000000000011
D=D+A 1110000010010000
@0 0000000000000000
M=D 1110001100001000
Instructions on how to convert assembly language to machine language;

Machine code:

The characters that start with double forward slashes are comments, so in our conversion we ignore
comments and spaces. These are called white spaces.

1. @2 – loads a non-negative binary equivalent 15 bit binary constant which is 0000000000000010

- Simply convert the decimal constant 2 to equivalent binary constant.

2. D=A – Sets D register to A register which is 1110110000010000

- This is a C-instruction with the syntax 1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3

- The first 3 bits are 111 of which C-instructions start with bits 111

- The computational numeric ‘a’ is 0 when we look at the reference diagram we are setting A
to D of which the computation is 110000.

- The destination of the result of computation is, referencing the diagram again we look at D
and we find its 010.

- Is there any jump? NO jump so the bits are 000.

- Combining the bits in machine language D=A is 1110110000010000.

3. @3 – a value of 3 is loaded in A register 0000000000000011

- We simply convert the decimal 3 to equivalent binary constant 0000000000000011

4. D=D+A - add A-register to D-register and set D-register to incremented value which is
1110000010010000

- Referencing the diagram, this is a C-instruction which in machine code the binary value
starts with bits 111

- The computational numeric is a = 0.

- Looking at the computational value of D+A is 000010

- The destination address is 010.

- There is no jump directive 000

- Combining the bits we have 1110000010010000

5. @0 – loads a zero in A register and the binary equivalent is 0000000000000000

6. M=D – sets RAM[A] to D-register is 1110001100001000

- This is a C instruction and it begins with bits 111

- Computational numeric a = 0
- Computational value is 001100

- Destination value is 001

- And without a jump directive is 000

- Combining the bits in machine language is 1110001100001000

Screen shot Image above is program 1 converted to machine language from Assembler software.

Converting program 2 from assembly to machine code.

// 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

Assembly Language Machine Language


@0 0000000000000000
D=M 1111110000010000
@1 00000000000000001
D=D-M 1111010011010000
@10 0000000000001010
D;JGT 1110001100000001
@1 00000000000000001
D=M 1111110000010000
@12 0000000000001100
0;JMP 1110101010000111
@0 0000000000000000
D=M 1111110000010000
@2 0000000000000010
M=D 1110001100001000
@14 00000000000001110
0;JMP 1110101010000111

1. @0 – Loads a constant zero in A-register whose equivalent value is 0000000000000000

- Simply convert decimal constant zero to binary constant 15bit zero.

2. D=M – sets D-register to RAM[A]

- This is a C-instruction whose starting bits are 111

- Referencing from the diagram the computational numerical value a = 1

- The computational value is 110000

- Destination value is 010

- And without a jump directive 000

- Combining the bits we have 1111110000010000

3. @1 – loads a 15 bit binary value in A-register

- Simply converting the decimal 1 value to 15 bit binary 00000000000000001

4. D=D-M – subtracts RAM[A] from D-register and sets D-register to the decremented value

- This is a C-instruction whose starting bits are 3 bits 111


- The computational numerical value is a=1

- The computational value is 010011 from the reference diagram.

- Its destination is 010

- And without a jump directive 000

- Combining the bits we have 1111010011010000

5. @10 – the A-register is loaded with the binary equivalent value of 10 in decimal

- We simply convert the decimal 10 to 15 bit binary value which is 0000000000001010

6. D;JGT – this is C-instruction which says jump to ROM in address A-register if D register is greater
than zero

- It begins with 3 bits 111

- The computational numeric is 0

- The computational value is 001100

- This computational has no destination 000

- And the jump directive is 001

- Reference from the diagram.

- Combining the bits we have 1110001100000001

7. @1 – loads a 15 bit binary value in A-register

- Simply converting the decimal 1 value to 15 bit binary 00000000000000001

8. D=M – sets D-register to RAM[A]

- This is a C-instruction whose starting bits are 111

- Referencing from the diagram the computational numerical value a = 1

- The computational value is 110000

- Destination value is 010

- And without a jump directive 000

- Combining the bits we have 1111110000010000

9. @12 – loads a 15 bit binary value in A-register

- Simply converting the decimal 12 value to 15 bit binary 0000000000001100


10. 0; JMP - Unconditional jump to ROM address in A register. Note that there must be a
computation; the convention is to use 0 for unconditional jumps

- The machine code begins with 111

- It computational numerical a= 0

- Computational value is zero 101010

- Without a destination 000

- Being unconditional jump to ROM 111

- Combining the bits we have 1110101010000111

11. @0 – Loads a constant zero in A-register whose equivalent value is 0000000000000000

- Simply convert decimal constant zero to binary constant 15bit zero.

12. D=M – sets D-register to RAM[A]

- This is a C-instruction whose starting bits are 111

- Referencing from the diagram the computational numerical value a = 1

- The computational value is 110000

- Destination value is 010

- And without a jump directive 000

- Combining the bits we have 1111110000010000

13. @0 – Loads a constant 2 in A-register whose equivalent value is 0000000000000010

- Simply convert decimal constant 2 to binary constant 15bit 2.

14. M=D – RAM[A] set to D-register

- The machine code begins with 111

- Whose computational numeric is a = 0

- Computational value is 001100

- Destination is RAM[A] 001

- Without a jump directive 000

- Combining the bit we have 1110001100001000

15. @14 – loads a 15 bit binary value in A-register

- Simply converting the decimal 14 value to 15 bit binary 00000000000001110


16. 0; JMP - Unconditional jump to ROM address in A register. Note that there must be a
computation; the convention is to use 0 for unconditional jumps

- The machine code begins with 111

- It computational numerical a= 0

- Computational value is zero 101010

- Without a destination 000

- Being unconditional jump to ROM 111

- Combining the bits we have 1110101010000111

Screen shot for program 2 convertion in assembler software.

Finally, I have learnt how physical hardware communicates or interact with software, and also the
under-laying principal of hardware and software interaction.

You might also like