Nand2Tetris Chapter4

2022. 9. 20. 16:10Computer Science/CPU

Nand2Tetris :  https://www.coursera.org/learn/build-a-computer

 

Build a Modern Computer from First Principles: From Nand to Tetris (Project-Centered Course)

히브리대학교에서 제공합니다. What you’ll achieve: In this project-centered course* you will build a modern computer system, from the ground up. We’ll divide ... 무료로 등록하십시오.

www.coursera.org


Chapter 4

Machine Language Programming

asm 코드 두 개를 만들어야 한다.

Chapter 4에서는 Hack 컴퓨터의 어셈블리 코드를 이용해 곱하기 코드랑, 키보드를 누르면 화면 전체가 검게 칠해지는 코드를 만들어야 한다.

 

1. Mult.asm

// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/04/Mult.asm

// Multiplies R0 and R1 and stores the result in R2.
// (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.)
//
// This program only needs to handle arguments that satisfy
// R0 >= 0, R1 >= 0, and R0*R1 < 32768.

	@res
	M = 0
	@i
	M = 1
	
(LOOP)
	@i
	D = M
	@R1
	D = D-M
	@LOOPEND
	D;JGT
	
	@R0
	D=M
	@res
	M = M+D
	
	@i
	M = M+1
	@LOOP
	0;JMP
	
(LOOPEND)
	@res
	D = M
	@R2
	M = D

 

2. Fill.asm

// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/04/Fill.asm

// Runs an infinite loop that listens to the keyboard input.
// When a key is pressed (any key), the program blackens the screen,
// i.e. writes "black" in every pixel;
// the screen should remain fully black as long as the key is pressed. 
// When no key is pressed, the program clears the screen, i.e. writes
// "white" in every pixel;
// the screen should remain fully clear as long as no key is pressed.

(MAINLOOP)
	@black
	M=0
	@KBD
	D=M
	@KBDFALSE
	D;JEQ
	@black
	M=-1
(KBDFALSE)
	@SCREENBLACK
	0;JMP


(SCREENBLACK)
	@SCREEN
	D=A
	@addr
	M=D
	
	@i
	M = 1
	
(SCREENLOOP)
	@i
	D = M
	@8192         // 전체 스크린 사이즈 8192개의 16비트
	D = D-A
	@MAINLOOP
	D;JGT
	
	@black
	D = M
	@addr
	A = M
	M = D
	@1
	D=A
	@addr
	M=D+M
	
	@i
	M = M+1
	@SCREENLOOP
	0;JMP

'Computer Science > CPU' 카테고리의 다른 글

Nand2Tetris Chapter6  (0) 2022.10.10
Nand2Tetris Chapter5  (0) 2022.09.22
Nand2Tetris Chapter3  (0) 2022.09.18
Nand2Tetris Chapter 2  (0) 2022.09.17
서론 + Nand2Tetris Chapter 1  (0) 2022.09.16