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

来源:互联网 发布:js防止ajax重复提交 编辑:程序博客网 时间:2024/05/18 01:42

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 0