Imprint | Privacy Policy

Machine and Assembly Language

(Usage hints for this presentation)

IT Systems, Summer Term 2024
Dr. Jens Lechtenbörger (License Information)

1. Introduction

1.1. Machine Languages

  • Duality
    • Machine language (= instruction set) as abstract description of computer architecture
      • Instructions as sequences of 0s and 1s
    • Hardware as physical means for realizing abstract machine language
  • Another duality
    • Machine language
      • Sequences of 0s and 1s
    • Assembly language (also called assembler)
      • Mnemonic notation (easier to remember, read, write for human beings)
      • Software called assembler translates assembly code to machine code

1.2. Today’s Core Question

  • How can a machine language for a computer embedding the Hack ALU look like?

1.3. Learning Objectives

  • Read and write programs in Hack assembly language

1.4. Retrieval Practice

Agenda

2. Aspects of the Hack Computer

2.1. Hack Computer

  • Hack computer

    Hack Computer

    Hack Computer” by user52174 under CC BY-SA 4.0; from StackExchange

    • Major components
      • CPU with registers A, D, PC
      • RAM, ROM
      • Input/Output (I/O) devices
        • Keyboard and screen
        • Memory maps for interaction and abstraction
      • Buses for connections
        • Addresses for data memory and instruction memory with 15 bits

2.2. I/O Handling: Screen

  • Programmer interacts with screen and keyboard through memory maps
  • Screen, output device
    • Black-and-white, 512 x 256 pixels, 1 bit per pixel

      • Pixels represented by binary values in memory map for screen
        • \(\frac{512 \cdot 256}{16} = 8192\) words
      • Memory map starting at address 16384
        • Each row with 512 pixels represented by \(\frac{512}{16} = 32\) consecutive 16-bit words
        • Least significant bit of each word represents left-most pixel

2.3. I/O Handling: Keyboard

  • Programmer interacts with screen and keyboard through memory maps
    • Keyboard, input device
      • Single word memory map at memory address 24576
      • Listen to keyboard: Read memory at address 24576
        • When key pressed: 16-bit ASCII code appears at 24576 (in addition to special codes, e.g., newline = 128)
        • When no key pressed: value 0 at address 24576

2.4. CPU Emulator

Screenshot of CPU Simulator for Nand to Tetris

Screenshot of CPU Simulator for Nand to Tetris” under GPLv2; from GitLab

  • Screenshot for student’s program
    • Note
      • Registers A, D, PC
      • Assembly code in ROM
      • Screen and its memory map in RAM

2.4.1. Video of CPU Emulator

  • Two routes are possible from here
    • Either you continue with this presentation for an explanation of the assembly language
    • Or you pause here and watch a video explaining major assembly language aspects using the CPU Emulator in Learnweb

3. Hack Assembly Language

3.1. Memory Access via A Register

  • Hack is 16-bit machine
    • 16 bits for words, registers, machine instructions
      • 6 bits for ALU operations, 15 bits for memory addresses
      • Thus, operation and address do not fit into one instruction
  • Hack uses implicit location “M” for memory access

    • “Operations” involving memory locations require 2 instructions
      1. Specify location “M”
      2. Operate on that location
    • Convention: M refers to memory word whose address is in A

      • In other words, M means Memory[A]
        • Therefore, A can be understood as address register
      • Example: “D = Memory[42] + 1” (D is useful as data register)
        1. Load 42 into A (A-instruction)
        2. Execute D = M + 1 (C-instruction)

3.2. A-Instruction

  • Simple syntax: @value
    • value is a non-negative number (or a symbol)
      • E.g., @42
    • Effect: Write value into A register
      • Thus, @42 writes 42 into A register

3.2.1. A-Instructions in 2 Steps

  • 2 steps as pairs of A-instruction and C-instruction (try out in CPU Emulator)

    • Assign constant to D register

      @42  // Write 42 into A register.
      D=A  // Copy contents of A register into D register.
      
    • Access RAM[42]

      @42  // Write 42 into A register.
      D=M  // M refers to RAM[A], which is RAM[42].
      	 // Thus, copy contents of RAM[42] to D.
      
    • Access ROM[42]

      @42  // Write 42 into A register.
      JMP  // JMP loads the PC with the value of the A register.
      	 // Thus, execution continues at ROM[42].
      

3.3. C-Instruction (Excerpt of Full Syntax)

  • On previous slide, D=A and D=M are C-Instructions

  • Assignments, more generally (excerpt of arithmetic calculations)
    • x in {A, D, M}, y in {A, D, M, 1}
    • dest in {null, M, D, MD, A, AM, AD, AMD}
      • dest = x+y
        • E.g., D=D+A, MD=M+1
      • dest = x-y
      • dest = x
      • dest = 0
      • dest = 1
      • dest = -1

Two issues:

  • What to compute?
  • Where to store the result?

3.4. Self-Study: Coding Exercise

  • Program using Hack instructions:
    • Set A to 17
    • Set D to A-1
    • Set both A and D to A + 1
    • Set D to 19
    • Set RAM[5034] to D - 1
    • Set RAM[53] to 171
    • Load RAM[7], add 1, and store the result in D
    • Increment the number stored in the RAM location whose address is stored in RAM[42]

If you get stuck, see here for solutions.

3.5. C-Instruction

dest=comp;jump // comp is mandatory; dest and jump are optional
  • Where
    • comp is one of

      0,1,-1,D,A,!D,!A,-D,-A,D+1,A+1,D-1,A-1,D+A,D-A,A-D,D&A,D|A,
      	       M,   !M,   -M,    M+1,    M-1,D+M,D-M,M-D,D&M,D|M
      
    • dest is one of

      null, M, D, MD, A, AM, AD, AMD
      
    • jump is one of

      null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP
      

3.5.1. (Conditional) Jumping

  • Jump loads the program counter (PC) with new location
    • Used for if and goto statements (e.g., jump to else part)
  • By convention, use only 2 types of jumps

    • Unconditional jump: 0;JMP
      • Thus, dest is null, comp is 0
      • Execution continues at ROM[A]
    • Conditional jumps refer to D register: D;JGT, D;JEQ, …

      • Thus, dest is null, comp is D

      • D;JGT jumps if value of D register is Greater Than 0

      • Similarly for D;JEQ (if EQual to 0); D;JGE (if Greater or Equal to 0), D;JLT (if Less Than 0), D;JNE (if Not Equal to 0), D;JLE (if Less than or Equal to 0)

3.5.2. Example: Max of 2 Numbers

// Write the larger of the two variables x and y to RAM[0].
// Idea: Compute y-x.  If result is >=0, then y is larger.
	  @x    // Load location of x to A.  Replaced by @16 in CPU Emulator.
	  D=M   // Load value of x to D.
	  @y    // Load location of y to A.  Replaced by @17.
	  D=M-D // Compute y-D (=y-x) to D.
	  @Y_IS_LARGER // Load Jump target to A.  Replaced by @10.
	  D;JGE        // If D is GE (greater or equal to 0), Jump.
		       // Otherwise, continue with next instruction.
	  // If we are here, x is larger.
	  // Load x to D and jump elsewhere for writing.
	  @x
	  D=M
	  @STORE_RESULT // Load Jump target to A.  Replaced by @12.
	  0;JMP         // Jump unconditionally.
(Y_IS_LARGER)   // Declare Jump target.
		// Next instruction (@y) is in ROM[10].
		// @Y_IS_LARGER is the address of that instruction.
		// If we are here, y is larger.  Copy via D to RAM[0].
	  @y
	  D=M
(STORE_RESULT)  // Declare Jump target.
		// Next two instructions write D to RAM[0].
	  @0
	  M=D
(END)           // Declare Jump target for end of program (ROM[14]).
		// By convention, infinite loops end programs in Hack.
	  @END  // Load Jump target to A.
	  0;JMP // Jump unconditionally to previous A-instruction.

(Source file)

3.5.3. Symbols

  • Symbol classes

    • Predefined

      • @SCREEN is the same as @16384 (first address of screen memory map)
      • @KDB is the same as @24576 (keyboard memory location)
      • (And more, not important for us)
    • Labels (in UPPER case by convention)

      • Destinations for JMP commands
      • Declared inside parentheses
        • E.g., (Y_IS_LARGER) and (END) above
        • Used without parentheses in A-instruction, e.g., @END
    • Variables (in lower case by convention)

      • Other symbols are treated as variables
        • E.g., x and y above
        • Translated to unique memory addresses by assembler and CPU Emulator, starting at RAM[16]

4. Hack Machine Language

4.1. A-Instruction

  • Assembler: @value
  • Binary: 16-bit number starting with 0
    • E.g., @420000000000101010

4.2. C-Instruction

Syntax of Hack C-Instructions

5. Conclusions

5.1. Summary

  • Machine language defines what a machine is able to do
    • Defines abstract interface for computer
      • Hack is 16-bit machine, 15 bits for addresses
      • With A-register, D-register, PC, implicit memory location M
    • Instructions as sequences of 0s and 1s
      • Two type of instruction for Hack: A-instructions and C-instructions
  • Assembly language as human readable variant
    • Including symbols for jump targets and variable

5.2. Self-Study-Tasks

  • Work on the above coding exercise
  • Implement “j=j+1” in Hack assembly (see Learnweb quiz)
  • Implement Mult.asm as part of Project 4 (ignore Fill.asm)

Bibliography

Nisan, Noam, and Shimon Schocken. 2005. The Elements of Computing Systems: Building a Modern Computer from First Principles. The MIT Press. https://www.nand2tetris.org/.

License Information

Source files are available on GitLab (check out embedded submodules) under free licenses. Icons of custom controls are by @fontawesome, released under CC BY 4.0.

Except where otherwise noted, the work “Machine and Assembly Language”, © 2024 Jens Lechtenbörger, is published under the Creative Commons license CC BY-SA 4.0.