MIPS入门

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

汇编语言的操作数都是(寄存器) registers
直接在硬件上实现的,数量有限 所有的操作都只能在寄存器上完成!
优势: 既然寄存器是直接用硬件实现的, 它 们一定很快! (faster than 1 billionth of a second)
6
Assembly Variables: Registers (2/4)
12
MIPS Addition and Subtraction (1/4) Syntax of Instructions:
1 2,3,4 where: 1) 操作名称(“操作符”) 2) 操作结果 (“目标”) 3) 1st 操作数 (“source1”) 4) 2nd 操作数 (“source2”)
-- comment, 2004-02-05
3
Instruction Set Architectures
早期的时候,人们不断地向CPU内添加各种 指令,以实现各种不同的甚至非常复杂的 操作 精简指令集RISC 哲学 (Cocke IBM, Patterson, Hennessy, 1980s) – Reduced Instruction Set Computing
立即数Immediates
Immediates are 数值常量. 他们在代码中频繁出现,所以专门针对立 即数定义了一些指令 Add Immediate:
addi $s0,$s1,10 (in MIPS) f = g + 10 (in C) where MIPS registers $s0,$s1 are associated with C variables f, g
19
Peer Instruction
A.Types are associated with declaration in C (normally), but are associated with instruction (operator) in MIPS. B.Since there are only 8 local ($s) and 8 temp ($t) variables, we can’t write MIPS for C exprs that contain > 16 vars. C.If p (stored in $s0) were a pointer to an array of ints, then p++; would be addi $s0 $s0 1
cmips有2种运算指令每一种指令又有两种数值方下面的可以检测出溢出异常addaddaddimmediateaddisubtractsub下面的不会检测出溢出异常addunsignedadduaddimmediateunsignedaddiusubtractunsignedsubu编译器会自动挑选合适的运算指令类型mips中的c编译器会使用adduaddiusubu不检查溢出异常46twologicinstructions这里再来两条新指令左移位
C 还有一种注释格式 /* comment */ 这种方式可以跨越多行加注释
11
Assembly Instructions
In assembly language, 每一条语句 (called an Instruction), 就是执行指令集 中的一条简单指令 Unlike in C (and most other High Level Languages), 每行汇编代码仅执行一条汇 编指令 Instructions are related to operations (=, +, -, *, /) in C or Java Ok, enough already…gimme my MIPS!
Notice: C中每一行语句可以拆成许多行的 MIPS. Notice: 编译时,每行的‖#‖后面的语句都 会被当成注释(comments)忽略
15
Addition and Subtraction of Integers (4/4) How do we do this? f = (g + h) - (i + j); 使用临时变量寄存器
我们约定, 每个寄存器都取一个名字以便写 代码时更方便 For now:
$16 - $23 $s0 - $s7 (与C语言中的变量对应) $8 - $15 $t0 - $t7 (与临时变量对应) 稍后我们会介绍其他16个寄存器
通常情况下, 我们使用名字来指定寄存器, 这样可以增加代码的可读性
计算机体系结构
刘昆 2009.11 徐海学院计算机系
汇编语言
CPU的基础工作: 执行许许多多的 指令 instructions. 执行指令是CPU的主要工作. 不同那个的CPU有不同的指令集. 我们将 一种特定的CPU所实现的一种指令集称为 指令集架构Instruction Set Architecture (ISA).
Example: sub $s3,$s4,$s5 (in MIPS) Equivalent to: d = e - f (in C) where MIPS registers $s3,$s4,$s5 are associated with C variables d, e, f
14
Subtraction in Assembly
9
C, Java variables vs. registers
In C (and most High Level Languages) 所有 变量需要事先声明成为一个特定的类型 int fahr, celsius; char a, b, c, d, e; 所有变量ONLY表达它被声明的那个类型的一个 值 (int与char用法不同,不能混淆). 在汇编语言里, 寄存器没有数值类型; 只能通过 我们的代码来决定这些寄存器中的数值应该如何 使用和处理
add $s0,$s1,$zero (in MIPS) f = g (in C) where MIPS registers $s0,$s1 are associated with C variables f, g
add $zero,$zero,$s0
17
既然是在硬件中定义的, so an instruction
add $t0,$s1,$s2# temp = g + h add $t1,$s3,$s4# temp = i + j sub $s0,$t0,$t1# f=(g+h)-(i+j)
16
Register Zero
一个特别的立即数, the number zero (0), 在代码中频繁出现. 所以我们定义 register zero ($0 or $zero) 永远只包含 0; eg
MIPS is simple, elegant.简单、 优雅,不被细节所累.
MIPS 在嵌入式中广泛应用, x86 很 少应用到嵌入式市场, 它更多的是应 用到PC上
5
Assembly Variables: Registers (1/4)
与高级程序设计语言C或Java不同, 汇编语 言不使用变量
Why not? 保持硬件的简单
Example:
10
Comments in Assembly
另一种增加代码可读性的方法: (注释) comments! Hash (#) 被用来做MIPS的注释
Note: Different from C.
任何从#(Hash Mark)后开始到行末的内容 都被视为注释内容,并被忽略 This is just like the C99 //
MIPS中,由32个二进制位组合在一起成为一 个 字
7
Assembly Variables: Registers (3/4)
我们从0到31给32个寄存器编号
当然,每个寄存器除了有编号外,还有自 己的名字 按照编号的引用方式:
$0, $1, $2, … $30, $31
8
Assembly Variables: Registers (4/4)
1 operator, 3 operands Why? 通过约定好的统一的规则使硬件实现更 简单
13
Syntax is rigid:
Addition and Subtraction of Integers (2/4) Addition in Assembly
Example: add $s0,$s1,$s2 (in MIPS) Equivalent to: a = b + c (in C) where MIPS registers $s0,$s1,$s2 are associated with C variables a, b, c
Examples: Intel 80x86 (Pentium 4), IBM/Motorola PowerPC (Macintosh), MIPS, Intel IA64, ...
2
Book: Programming From the Ground Up
“A new book was just released which is based on a new concept - teaching computer science through assembly language (Linux x86 assembly language, to be exact). This book teaches how the machine itself operates, rather than just the language. I've found that the key difference between mediocre and excellent programmers is whether or not they know assembly language. Those that do tend to understand computers themselves at a much deeper level. Although [almost!] unheard of today, this concept isn't really all that new -- there used to not be much choice in years past. Apple computers came with only BASIC and assembly language, and there were books available on assembly language for kids. This is why the old-timers are often viewed as 'wizards': they had to know assembly language programming.”
Syntax similar to add instruction, except that 最后一个参数用数值代替了 寄存器.
18
Immediates
MIPS中没有立即数的减法: Why? 我们尽可能地保持指令集越小越好
如果一个操作可以解开成一些简单指令,就不 要将该操作包含进指令集 addi …, -X = subi …, X => so no subi addi $s0,$s1,-10 (in MIPS) f = g - 10 (in C) where MIPS registers $s0,$s1 are associated with C variables f, g
劣势: 既然寄存olution: MIPS code 必须很仔细的写,以便 能够很合理、很有效的使用寄存器
32 registers in MIPS
Why 32? 简单源自规整!
Each MIPS register is 32 bits wide
VAX 竟然曾经有一条专门计算多项式的指令!
保持指令集“小并简单着”, 这样的话,我们 可以更加简单的设计出速度非常快的芯片. 实用软件去实现各种复杂操作,将一系列简单 操作排列组合即可.
4
MIPS Architecture
MIPS – 最早一家生产出商用 RISC architectures的半导体 公司 我们将会学习MIPS architecture 的一些细节 Why MIPS instead of Intel 80x86?
Addition and Subtraction of Integers (3/4) How do the following C statement? a = b + c + d - e; 拆成多条汇编指令
add $t0, $s1, $s2 # temp = b + c add $t0, $t0, $s3 # temp = temp + d sub $s0, $t0, $s4 # a = temp - e
相关文档
最新文档