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

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 閱讀(465) 評論(0)  編輯 收藏 引用 所屬分類: 拿來主義
<2006年12月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

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

常用鏈接

留言簿(4)

隨筆分類(70)

隨筆檔案(71)

charles推薦訪問

搜索

  •  

積分與排名

  • 積分 - 51914
  • 排名 - 448

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            欧美不卡激情三级在线观看| 亚洲国产精品成人一区二区| 国产目拍亚洲精品99久久精品| 亚洲永久精品大片| 香蕉尹人综合在线观看| 亚洲精品久久久久久下一站| 久久亚洲图片| 久久综合狠狠综合久久综青草| 久久免费国产精品| 开心色5月久久精品| 亚洲福利电影| 中日韩美女免费视频网址在线观看| 在线欧美影院| 亚洲伦理一区| 一区二区欧美国产| 亚洲欧美日韩一区在线观看| 欧美在线视频播放| 免费在线欧美视频| 亚洲人成小说网站色在线| 亚洲人成在线观看| 亚洲欧美日韩综合aⅴ视频| 欧美在线视频全部完| 亚洲私拍自拍| 久久成人18免费观看| 久久综合久久综合这里只有精品 | 久久久亚洲人| 亚洲免费影视| 亚洲一区二区三区视频播放| 羞羞色国产精品| 免费成人高清在线视频| 一区二区激情| 久久久水蜜桃| 国产精品久久久久影院亚瑟| 在线视频成人| 国产精品日本一区二区| 尤物九九久久国产精品的分类| 一区二区国产精品| 久久先锋资源| 一区二区三区高清在线观看| 久久嫩草精品久久久精品| 欧美日韩麻豆| 亚洲国产精品va在线看黑人 | 国产乱肥老妇国产一区二| 亚洲激情网址| 久久久久久9999| 妖精成人www高清在线观看| 久久精品在线观看| 欧美性感一类影片在线播放| 亚洲欧洲日本国产| 久久午夜精品一区二区| 亚洲一区二区三区四区在线观看 | 91久久精品www人人做人人爽| 亚洲小说春色综合另类电影| 免费久久精品视频| 国产午夜精品一区二区三区视频| av成人免费在线观看| 蜜桃av久久久亚洲精品| 先锋影音国产一区| 蜜桃av一区二区在线观看| 国产精一区二区三区| 亚洲欧美一级二级三级| 日韩视频在线永久播放| 欧美了一区在线观看| 亚洲精品视频在线| 亚洲国产日韩欧美| 免费91麻豆精品国产自产在线观看| 国产精品久久999| 亚洲在线视频网站| 亚洲视频axxx| 国产精品久久一区二区三区| 亚洲五月婷婷| 一区二区三区欧美| 国产精品青草综合久久久久99 | 欧美性理论片在线观看片免费| 欧美日韩理论| 国产在线精品成人一区二区三区| 亚洲综合另类| 亚洲一级高清| 国产喷白浆一区二区三区| 欧美在线观看日本一区| 午夜久久福利| 在线不卡中文字幕| 亚洲福利电影| 国产精品国产自产拍高清av王其| 欧美在线免费看| 欧美一区二区在线| 亚洲欧洲综合另类| 亚洲一区二区三区四区五区黄| 国产九九精品视频| 女主播福利一区| 国产精品久久久久久久久 | 妖精成人www高清在线观看| 欧美色欧美亚洲高清在线视频| 欧美一区91| 欧美成人午夜免费视在线看片 | 在线欧美日韩| 最新成人在线| 国产欧美日韩91| 欧美大片91| 国产精品主播| 91久久精品日日躁夜夜躁欧美| 国产一区二区三区av电影| 欧美**人妖| 亚洲美女黄色片| 亚洲经典一区| 亚洲精品女av网站| 欧美老女人xx| 亚洲人www| 一本色道久久综合亚洲二区三区| 国产精品伦一区| 久久免费视频观看| 欧美色视频一区| 久久精品国产一区二区电影| 久久男人资源视频| 亚洲狼人综合| 久久久www成人免费无遮挡大片| 精品盗摄一区二区三区| 久久影院午夜片一区| 久久精品视频在线免费观看| 日韩视频一区| 亚洲欧美高清| 亚洲国产欧美一区二区三区丁香婷| 亚洲国产成人porn| 国产美女精品视频| 亚洲国产精品久久久久秋霞影院| 亚洲黄色av一区| 国产精品jvid在线观看蜜臀| 久久精品国产清自在天天线| 免费观看久久久4p| 猛干欧美女孩| 国产精品夜色7777狼人| 欧美1区2区视频| 欧美午夜精品一区| 牛牛影视久久网| 国产精品国产三级国产aⅴ无密码| 久久成人免费网| 欧美精品在线观看一区二区| 一本久久综合| 亚洲视频www| 亚洲精品免费在线观看| 欧美在线免费观看| 亚洲欧美精品| 欧美黄色视屏| 麻豆av一区二区三区| 国产农村妇女精品| 99国内精品| 亚洲激情专区| 欧美日韩高清一区| 鲁大师成人一区二区三区| 国产精品99免费看 | 亚洲一区二区日本| 欧美成人日本| 亚洲欧美日韩视频一区| 国产伦精品一区二区三区高清| 亚洲国产视频一区二区| 一区二区三区中文在线观看| 一级成人国产| 中国成人在线视频| 欧美人与性动交α欧美精品济南到| 久久久久久久性| 国产免费观看久久黄| 在线天堂一区av电影| 中国成人亚色综合网站| 久久久久国产成人精品亚洲午夜| 久久资源在线| 在线日韩中文字幕| 免费不卡在线观看| 欧美激情久久久| 国产视频一区在线观看一区免费| 久久国产一区| 毛片av中文字幕一区二区| 国产日韩在线看| 久久久久国产精品厨房| 久久精品盗摄| 亚洲免费观看在线观看| 欧美gay视频| 精品电影在线观看| 玖玖在线精品| 亚洲国产成人91精品| 日韩视频在线免费| 国产精品国产三级国产普通话蜜臀 | 国产麻豆日韩欧美久久| 亚洲激情亚洲| 久久久久.com| 一区二区三区自拍| 久久免费精品视频| 亚洲国内欧美| 另类尿喷潮videofree| 一本久久综合亚洲鲁鲁| 欧美亚洲一区在线| 影音先锋一区| 欧美精品免费播放| 亚洲伊人第一页| 女人香蕉久久**毛片精品| 亚洲一区高清| 日韩视频亚洲视频| 久久大逼视频| 亚洲激情女人| 国产精品毛片在线| 欧美一区二区三区男人的天堂|