《计算机系统要素》学习笔记:第四章机器语言

1.学习要点
1)机器语言可以被看作是一种约定的形式,它利用处理器寄存器来操控内存
2)汇编语言可分为三类:内存访问(直接寻址,立即寻址,间接寻址),算术逻辑运算,分支跳转语句(流程控制)
3)hack语言,共有A-指令和C-指令两种指令,A指令负责寻址,C指令负责运算和跳转,与第五章的CPU结构对应。
4)乘法程序用多次加法来实现。
5)I/O处理程序没看懂,网络找到答案,但是模拟器上效果不对。。。(+_+)

2.代码实现
(1)乘法程序

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

@0  
D=M  
@2  
M=0  
(LOOP)  
@1  
D=M  
@END  
D;JEQ  

@1  
M=M-1  

@0  
D=M  
@2  
M=M+D  

@1  
D=M  
@LOOP  
D;JGT  

@END  
(END)  
0;JMP 

(2)I/O处理程序

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

// Put your code here.

(BEGIN)  

@KBD  
D = M  
@CLEAN  
D;JEQ  

@SCREEN          //涂黑   
D = A  
@Bcount  
A = M + D  
M = -1  

@Bcount          //键盘按下  
M = M + 1  
D = M  
@8192            //8K  
D = D - A  
@Bcount  
M = D  
@END  
D;JGE  
@8192  
D = D + A  
@Bcount  
M = D  
@END  
0;JMP  

(CLEAN)          //无键盘按下   

@SCREEN          //涂白   
D = A  
@Wcount  
A = M + D  
M = 0  

@Wcount          //键盘按下  
M = M + 1  
D = M  
@8192            //8K  
D = D - A  
@Wcount  
M = D  
@END  
D;JGE  
@8192  
D = D + A  
@Wcount  
M = D  

(END)  
@BEGIN  
0;JMP  
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值