青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

posts - 71,  comments - 41,  trackbacks - 0
? 2003 by Charles C. Lin. All rights reserved.

Background

You should know what UB and 2C representation is. You should also know about sign-extension.

ISA

When you started learning how to program, you were told that your program had to be compiled. That is, it had to be converted from a high-level language into a low-level language. For C and C++, the low-level language is basically machine code.

An ISA defines the machine and assembly code used by a CPU.

ISA stands for "Instruction Set Architecture". Effectively, the ISA is the programmer's view of the computer.

An ISA consists of:

  • The instruction set This is the set of instructions supported. This is the part that's usually called the assembly language.
  • The register set This is the set of registers you can use. (There are other hidden registers which you can't use directly. They are used indirectly, however).
  • The address space This is the set of memory addresses that can be used by your program.

The ISA is basically a hardware specification. It's the view of the hardware as seen by an assembly language programmer.

The ISP (instruction set processor) is an implementation of the ISA. There may be many implementations for a given ISA. For example, IA32 is the instruction set architecture for x86 processors. Intel has the Pentium and Celeron lines of CPUs that implement this ISA. AMD also has its own CPUs that implement the ISA. Each implementation is different, but they all run code written in IA32.

Why You Need to Know About Instructions

We study instruction sets because that's what CPUs process. They run one instruction after another. In order to understand how a computer works, you need to know what instructions are, and more importantly, how to write them.

There are two ways to write instructions. Either you can write them in assembly language, which is human-readable. Or you can write them in machine code, which is basically, 0's and 1's. CPUs process machine code, but humans usually program in assembly language.

You need to know both, in order to understand how a CPU works.

The MIPS ISA

There are two ways to write instructions. You can write it in assembly language, which is human readable, or you can write it in machine code, which is 0's and 1's. For MIPS32, each machine code instruction is a 32-bit bitstring.

The "32" in MIPS32 refers to the size of the registers (i.e., how many bits each register holds) and to the number of bits used in an address. There is also a MIPS64, which has 64 bit addresses and 64 bit registers.

The MIPS32 architecture contains 32 general purpose int registers. The registers are named $r0, $r1, ..., $r31. Each register can store 32 bits. Most of the times the registers either store signed or unsigned ints. However, sometimes they store addresses, and occasionally ASCII characters, etc.

MIPS also has 32 floating point registers, but we won't worry about them too much.

Unlike programming languages where you can declare as many variables as you want, you can't create any more registers. The number of registers doesn't change.

MIPS32 allows you to access data in memory using 32 bit addresses. In principle, you can access up to 232 different addresses, using 32 bits. In practice, some of those addresses may be invalid. For example, the CPU may simply not have that much memory (232 addresses is 4 GB). Thus, you might be able to generate the 32-bit address, but there may be nothing stored at that address (an error usually occurs when you access an invalid address).

In MIPS, nearly all registers are general purpose. You can classify ISAs into those that use general purpose registers (i.e., instructions can refer to any register---all registers perform the same operations) or special purpose (certain instructions can only be used on specific, i.e., not all, registers).

However, there is at least one exception. $r0 is not general purpose. It is hardwired to 0. No matter what you do to this register, it always has a value of 0. You might wonder why such a register is needed in MIPS.

The designers of MIPS used benchmarks (programs used to determine the performance of a CPU), which convinced them that having a register hardwired to 0 would improve the performance (speed) of the CPU as opposed to not having it. Not everyone agrees a register hardwired to 0 is essential, so not all ISAs have a zero register.

Assembly vs. Machine Code

CPUs process binary bitstrings. These bitstrings are really instructions, encoded in 0's and 1's. When people began to write programs for computers, they wrote it in binary. That is, programs were written in 0's and 1's. The code probably looked something like this:

0000 0000 0101 1000 0000 0000 0101 1000
1010 1101 0000 1011 1000 1100 1001 0110

This is called machine code.

As you might imagine, machine code was difficult to read and difficult to debug. The amount of time wasted trying to find whether you had accidentally written a 0 instead of a 1, lead to the invention of assembly language.

Assembly language is a somewhat more human-readable version of machine code. For example, assembly code might look something like:

add   $r2, $r3, $r4
addi  $r2, $r3, -10

While you may not understand the code above, you've certainly got a much better chance of figuring it out than the machine code equivalent. Each line of assembly code contains an instruction. Each instruction tells the computer one small task to accomplish. Instructions are the building blocks of programs.

CPUs can't handle assembly code directly. Instead, assembly code is translated to machine code. If this sounds like compiling, that's because it basically is comiling. However, people usually call the process of translating assembly to machine code assembling, instead of compiling.

You'll write code in assembly, and learn how to translate some instructions from assembly to machine code. It's very important that you understand the machine code, because that's what the CPU processes. Furthemore, by studying machine code, you get to see how information is encoded into 0's and 1's, and you get to see how the CPU uses these binary values to execute the instruction in hardware.

Encoding Registers

In the previous set of notes, we talked about how many bits you needed to create N different labels. We assume each label has k bits long.

You need k = ceil( lg N ) bits to uniquely label N items.

MIPS32 has 32 integer registers. We want to label each register by a number, so instructions can refer to registers by number. Since MIPS has 32 registers, you need ceil( lg 32 ) = 5 bits.

If we think of the 5 bit numbers as unsigned binary numbers, then the registers are numbered from 0 up to 31, inclusive. In fact, that's exactly how MIPS numbers its registers. Registers are numbered from $r0 up to $r31. The binary equivalent are numbered from 00000 to 11111.

In assembly language, you'd write $r6. In machine code, you'd write the same register as: 00110. In assembly language, you'd write $r30. In machine code, you'd write 11110.

This is important because we're going to use register encoding in the machine language instructions for MIPS. Recall that machine code is a 32-bit bitstring. When we refer to registers within the instruction, it's going to be using the 5 bit binary numbers written in UB (unsigned binary).

What is an instruction?

An assembly language instruction is basically a function call. Like C functions, assembly language instructions have a fixed number of arguments. You can't add or remove the number of arguments.

Like C functions, arguments of assembly language instructions have type. Or at least, something that resembles type. Basically, there are 4 kinds of "types" for MIPS.

  • Registers ($r0, $r1,..., $r31)

  • Immediates Constants, such as, 10, -20, etc. Sometimes written in hexadecimal, e.g., 0x3a.

  • Register Offset This is a constant and a register, written as -10($r3) or 214($r4). That is, you write the immediate (constant) value, then a left parenthesis, then a register, then a right parenthesis.

    The computation is performed by adding the contents of the register to the offset, usually resulting in a 32 bit address. Thus, -10($r3) is -10 added to the contents of register 3. This result is "temporary" and register 3 is not modified (just like x + y in a programming language merely adds x to y, but the sum does not change x or y

  • Labels There are identifiers to locations in memory. Generally, you write labels in uppercase letters and underscores, such as FOR_LOOP.

For the most part, we'll only consider registers and immediate values.

Let's consider two examples of instructions and their operands:

  • add $r2, $r3, $r4 This instruction adds the contents of register 3 and register 4, and places the result in register. It's basically R[2] = R[3] + R[4], if you pretend that the registers form an array.

  • addi $r2, $r3, -10 This instruction adds the contents of register 3 to -10 and places the result in register 2. It's basically R[2] = R[3] - 10

The first instruction is an add instruction. add requires exactly 3 operands (arguments). Each operand must be a valid register. The operand can not be anything besides a register. In particular, you can not create expressions such as:

# WRONG! Operands can't be expressions
add $r2, $r3, (add $r4, $r5, $r6) 
The second instruction is an addi instruction. addi must also have three operands. The first two operands must be registers, while the third one must be an integer between -215 to 215 - 1, inclusive.

There is a reason for this restriction in value, which we will discuss momentarily.

Unlike higher level programming languages, you can't create new registers. You're forced to use the ones available. You can't create new instructions either. You must use the ones provided in the instruction set.

Machine Code

A machine language instruction ususally consists of:

  • opcode This is a binary representation of the instruction. For example, an add instruction has an opcode of 000 000.
  • operands Operands means the same thing as arguments. It's older terminology usually associated with assembly/machine code instructions.

MIPS divides instructions into three formats. Instructions are either R-type (register type), I-type (immediate type), or J-type (jump type). The types refer to the format, not to its purpose. (For example, branch instructions are I-type, because of its format, even if it would seem like it should be J-type).

Here are the layouts of the three kinds of instructions.

R-type Instruction

OpcodeRegister sRegister tRegister dShift AmtFunction
B31..26B25..21B20..16B15..11B11..6B5..0
ooo ooossssstttttdddddaaaaaffffff

  • R-type instructions are short for "register type" instructions.
  • Bits B31..26 are used for the opcode. For R-type instructions, the opcode is almost always 000 000. Normally, this makes no sense, because every instruction should have a unique opcode. However, bits B5..0 (the function part) uses 6 bits to specify the instruction. Only R-type instruction uses a function.
  • Bits B25..21 specify a 5-bit UB encoding for the first source register.
  • Bits B20..16 specify a 5-bit UB encoding for the second source register.
  • Bits B15..11 specify a 5-bit UB encoding for the destination register. This specifies which register stores the result of the operation.
  • Bits B11..6 specify the shift amount. This is usually 00000, except for shift instructions.
  • Bits B5..0 specify a 6-bit function. Each R-type instruction has a unique 6 bit value. For example, add has a 6-bit value that's different from sub. add and sub are two different instructions.

I-type Instruction

OpcodeRegister sRegister tImmediate
B31..26B25..21B20..16B15..0
ooo ooossssstttttiiii iiii iiii iiii

  • I-type instructions are short for "immediate type" instructions.
  • Bits B31..26 are used for the opcode. Unlike R-type instructions, the 6-bit value is NOT 000 000. There is no function code for I-type instructions.
  • Bits B25..21 specify a 5-bit UB encoding for the source register.
  • Bits B20..16 specify a 5-bit UB encoding for the destination register. Although this is called register t, instead of register d, it is treated as the destination register for I-type instructions.
  • Bits B15..0 is the 16-bit immediate value. This may be a 16-bit UB number or a 16-bit 2C number. Notice that the immediate value is encoded directly into the instruction.

J-type Instruction

OpcodeTarget
B31..26B25..0
ooo ooott tttt tttt tttt tttt tttt tttt

  • J-type instructions are short for "jump type" instructions.
  • Bits B31..26 are used for the opcode. Unlike R-type instructions, the 6-bit value is NOT 000 000. There is no function code for J-type instructions.
  • Bits B25..0 are used for the offset. This is usually used to generate an address.

Notice that the J-type instruction has no source or destination registers.

add, an R-type instruction

The general format for an add instruction is:
add $rd, $rs, $rt
$rd, $rs, and $rt are not real registers. They are merely place holders. For example, if we write add $r2, $r3, $r4, then for this particular example, $rd = $r2, $rs = $r3, and $rt = $r4.

In assembly language, the instructions are written with the destination register (i.e. register d), then the first source register, (i.e. register s) then the second source register (i.e. register t).

Note: This is NOT the same order as it is written in machine code. In assembly, it's destination, source 1, source 2. In MIPS machine code, it's written source 1, source 2, destination.

Don't ask me why the MIPS folks did it that way. They just did.

Let's translate the following instruction into MIPS assembly.

add $r2, $r3, $r4

For add, the opcode is 000 000. The function code is 100 000. Since the shift amount isn't used, it's set to 00000.

We encode $r2 as 00010, $r3 as 00011, and $r4 as 00100.

This is how the machine code equivalent looks:

OpcodeRegister sRegister tRegister dShift AmtFunction
B31..26B25..21B20..16B15..11B11..6B5..0
? $r3$r4$r2? ?
000 00000011001000001000000100 000

Again, notice that bits B25..21 is source 1 (i.e., $r3), then B20..16 is source 2 (i.e., $r4), then B15..11 is the destination register (i.e., $r2).

It's important that you learn how to translate a few instructions, because the CPU manipulates the binary version of this, not the assembly version. In particular, pay attention to how the registers are encoded, and just as importantly, which bits refer to which registers.

addi, an I-type instruction

addi stands for add immediate. It's an I-type instruction.

The general format for an addi instruction is:

addi $rt, $rs, IMMED
For I-type instructions, $rt is the destination register (not $rs). $rs is still the first source register. For addi, the immediate value is written in base 10 (or possibly, hexadecimal), but it eventually gets translated to 2C.

Let's look at a specific example.

addi $r3, $r10, -3

This instruction adds the contents of register 10, to the value -3, and stores the result in register 3.

The opcode for addi is 001 000. In 2C, you write -3ten as 1111 1111 1111 1101.

This is how the instruction is encoded.

OpcodeRegister sRegister tImmediate
B31..26B25..21B20..16B15..0
? $r10$r3-10, represented in 2C
001 00001010000111111 1111 1111 1101

Again, notice that in the assembly code $r3 (i.e., the destination register) appears first, while in the machine code $r3 appears second. Also, notice that the immediate value is written in 16 bits, two's complement.

Now that you see why it's written in 16 bits, 2C, you see why the immediate value can only be between -2-15 through 215 - 1. This is the range of valid values for 16 bit 2C.

The assembler must translate base 10 representation to 2C representation when translating addi from assembly to machine code.

Some instructions encode the immediate in 2C, while other instructions encode it in UB.

Summary

This section on instructions is not trying to teach you how to program in MIPS assembly. Instead, it's to briefly introduce you to what an instruction is, and how it is encoded.

While it's useful to know how to program in MIPS assembly, it's isn't essential to understand how a CPU works. To understand how a CPU works, at least, initially, all you need to know is what an instruction looks like in binary, and what that individual instruction is supposed to do.

posted on 2007-01-23 15:42 Charles 閱讀(460) 評論(0)  編輯 收藏 引用 所屬分類: 拿來主義
<2007年1月>
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910

決定開始寫工作日記,記錄一下自己的軌跡...

常用鏈接

留言簿(4)

隨筆分類(70)

隨筆檔案(71)

charles推薦訪問

搜索

  •  

積分與排名

  • 積分 - 51563
  • 排名 - 449

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美一级久久久久久久大片| 日韩系列欧美系列| 欧美一区二区三区另类| 亚洲国产日韩一区| 中日韩高清电影网| 在线免费观看视频一区| 激情国产一区| 一本色道久久精品| 欧美一级专区| 麻豆精品视频在线观看| 久久久五月天| 久久嫩草精品久久久精品| 欧美一区二区三区的| 免费欧美视频| 国产精品国产三级国产普通话三级 | 国产精品爱啪在线线免费观看| 国产精品成人一区二区网站软件 | 亚洲一区二区精品视频| 久久福利影视| 欧美特黄视频| 伊人久久综合97精品| 一二美女精品欧洲| 久久五月天婷婷| 在线亚洲欧美专区二区| 久久精品国产精品亚洲综合| 欧美日韩国产经典色站一区二区三区| 国产精品乱码妇女bbbb| 亚洲激情欧美| 久久成人免费电影| 亚洲日韩欧美视频一区| 久久精品日产第一区二区| 欧美视频久久| 日韩视频―中文字幕| 久久噜噜噜精品国产亚洲综合| 91久久午夜| 久久免费视频在线观看| 国产精品亚洲аv天堂网| 亚洲日韩欧美视频| 美女图片一区二区| 欧美一区免费视频| 国产精品一区二区久激情瑜伽 | 久久久久国产精品厨房| 在线视频亚洲一区| 欧美精品导航| 最新高清无码专区| 欧美肥婆在线| 久久综合色天天久久综合图片| 国产欧美一区二区三区久久人妖| 亚洲视频专区在线| 日韩亚洲欧美高清| 欧美区一区二区三区| 亚洲毛片在线| 欧美激情亚洲自拍| 欧美91福利在线观看| 亚洲片国产一区一级在线观看| 欧美jizzhd精品欧美巨大免费| 久久精品免费观看| 在线观看亚洲视频啊啊啊啊| 久久这里有精品15一区二区三区| 午夜精品久久久久| 国产日韩欧美夫妻视频在线观看| 久久精品国产精品亚洲综合| 国产老肥熟一区二区三区| 亚洲第一偷拍| 99riav久久精品riav| 亚洲片国产一区一级在线观看| 你懂的国产精品| 久久噜噜噜精品国产亚洲综合| 国内外成人在线| 欧美阿v一级看视频| 免费一区二区三区| 亚洲乱码国产乱码精品精天堂| 亚洲国产精品成人综合色在线婷婷| 男女视频一区二区| 亚洲视频在线二区| 欧美一区三区三区高中清蜜桃| 精品二区久久| 亚洲国产裸拍裸体视频在线观看乱了 | 亚洲欧美国产三级| 国产一区二区日韩精品欧美精品| 久久久久**毛片大全| 久久频这里精品99香蕉| 夜夜嗨av色一区二区不卡| av不卡在线看| 激情六月综合| 亚洲精品黄网在线观看| 国产精品视频精品| 久久综合婷婷| 国产精品mm| 欧美成人精品不卡视频在线观看 | 亚洲大胆av| 亚洲特黄一级片| 一色屋精品视频在线看| 99这里只有精品| 黄色成人av| 99爱精品视频| 亚洲第一中文字幕| 亚洲制服欧美中文字幕中文字幕| 亚洲第一精品福利| 亚洲一区二区三区在线播放| 亚洲国产精品久久久久秋霞影院 | 国产精品一区二区三区免费观看 | 可以看av的网站久久看| 欧美激情一区二区三区| 欧美在线观看视频一区二区三区| 久久香蕉国产线看观看网| 午夜精品在线看| 欧美日韩国产成人精品| 裸体女人亚洲精品一区| 国产精品久久久久aaaa九色| 亚洲人成绝费网站色www| 国内自拍视频一区二区三区| 在线中文字幕不卡| 91久久国产综合久久| 欧美一区二区三区视频| 一本色道久久加勒比88综合| 久久精品免费看| 香蕉av777xxx色综合一区| 欧美精品v日韩精品v韩国精品v| 久久精品一区四区| 国产精品每日更新| 一本色道久久精品| 日韩写真视频在线观看| 免费日韩av电影| 免费成人你懂的| 经典三级久久| 欧美中文字幕不卡| 久久精品夜色噜噜亚洲a∨| 国产精品稀缺呦系列在线| 一区二区三区 在线观看视频| 99re6这里只有精品| 欧美日本精品| 亚洲美女毛片| 中文精品视频一区二区在线观看| 欧美极品aⅴ影院| 亚洲另类自拍| 在线视频精品一| 欧美午夜美女看片| 亚洲在线一区二区三区| 久久精品国产亚洲精品 | 毛片一区二区| 欧美激情女人20p| 日韩视频在线观看| 欧美日韩精品一区二区三区四区| 亚洲免费观看高清完整版在线观看| 亚洲精品自在久久| 欧美日一区二区在线观看 | 亚洲国产视频一区二区| 麻豆国产精品va在线观看不卡| 欧美高清不卡| 日韩亚洲国产精品| 欧美天堂亚洲电影院在线观看| 一区二区日本视频| 久久精品91| 亚洲激情啪啪| 国产精品日日摸夜夜添夜夜av| 亚洲欧美日韩国产精品| 看欧美日韩国产| 99成人精品| 国产精品久久久久一区二区| 亚洲欧美日本日韩| 欧美第一黄网免费网站| 99热这里只有精品8| 国产精品免费一区二区三区观看| 欧美一区激情| 亚洲国产欧美日韩| 欧美在线一级va免费观看| **性色生活片久久毛片| 欧美偷拍一区二区| 久久国产精品99精品国产| 亚洲国产精品精华液2区45| 国产精品99久久久久久有的能看 | 亚洲日本无吗高清不卡| 午夜精品一区二区三区电影天堂| 精品不卡在线| 欧美激情精品久久久| 国产亚洲高清视频| 嫩草伊人久久精品少妇av杨幂| 一本大道久久a久久综合婷婷 | 久久综合九九| 亚洲无吗在线| 亚洲福利精品| 国产日韩欧美在线视频观看| 欧美劲爆第一页| 久久久激情视频| 亚洲在线视频网站| 亚洲国产一区二区a毛片| 久久黄金**| 亚洲影院在线| 夜夜爽www精品| 亚洲电影在线看| 国产网站欧美日韩免费精品在线观看| 欧美sm极限捆绑bd| 久久精品水蜜桃av综合天堂| 亚洲视频导航| 一区二区高清在线观看| 亚洲片在线资源| 欧美激情bt| 欧美国产日韩亚洲一区| 久久综合中文字幕|