Académique Documents
Professionnel Documents
Culture Documents
Today we look at registers, instruction formats, some special instructions, system calls, directives, labels, p , y , , , comments, some examples, and a couple of exercises.
2 Lecture #11 Registers, System Calls, and More Instructions
N. B. Dodge 09/09
Registers
3
We remember that registers are collections of D flip-flops. Registers therefore can hold N-bit binary numbers, where N is the number of ffs in each register ff s register. The number of register bits (width) depends on the computer. The MIPS R-2000 computer has 32-bit data words, so that each register has 32 fli fl it h flip-flops. The MIPS computer has 32 fixed-point data registers. There are rules (actually suggestions or policies) for register use. The rules may be violated; the assembler has no Register Police. However the violator does so at his/her own risk usage violations can result in incompatible p g p programs, which is very bad p y practice. In EE 2310, you must use registers as recommended in the rules.
Lecture #11 Registers, System Calls, and More Instructions
N. B. Dodge 09/09
1 Dont ever use these registers! 2 We will not use this register. Don t register
5 Lecture #11 Registers, System Calls, and More Instructions
N. B. Dodge 09/09
Name
$t0 $t1 $t2 $t3 $t4 $t5 $t6 $t7 $t8 $t9
Purpose p
Holds a local (temp) variable Holds a local (temp) variable Holds a local (temp) variable Holds l l (t H ld a local (temp) variable ) i bl Holds a local (temp) variable Holds a local (temp) variable Holds a local (temp) variable ( p) Holds a local (temp) variable Holds a local (temp) variable Holds a local (temp) variable
Name
$s0 $s1 $s2 $s3 $s4 $s5 $s6 $s7
Purpose
Holds a saved temporary variable Holds a saved temporary variable Holds a saved temporary variable p y Holds a saved temporary variable Holds a saved temporary variable Holds a saved temporary variable Holds a saved temporary variable Holds a saved temporary variable
NOTE: In a stand alone program, both s- and t-registers may be used stand-alone s t registers to store temporary variables.
Lecture #11 Registers, System Calls, and More Instructions
N. B. Dodge 09/09
Instruction Processing
In all computers, an instruction is retrieved from memory, the fields of the instruction are decoded, and the decoded commands are processed.
I Instruction
Decoders
Result (to Registers or Memory)
Registers
Decoders
Instruct tion
Memory
Register Block
ALU
Instr.
Decoders
N. B. Dodge 09/09
Instruction Formats
There are only three basic MIPS instruction formats: Register (as seen in last lecture), Immediate, and Jump. Jump is shown below.
Bit 31 Bit 0
6 bits
26 bits
Op Code Field (tells computer what to do when decoded, in this case, jump). case jump)
9
Address Field (added to a part of the Program Counter register contents to create the 32-bit jump address).
N. B. Dodge 09/09
6 bits
5 bits 5 bits
16 bits
Address Component Field ( p (16-bit signed g number). In a load or store, added to the contents of register Rs to create 32-bit address in memory from which data is loaded or to which data is stored. Added to PC contents when a branch is executed.
N. B. Dodge 09/09
Register-Register Instructions
For most R-type instructions, the sixbit op code is 0x0 (00 0000) 0000). This 5-bit number tells the ALU the shift amount in a shift instruction. Rs = 0 for shifts Bit 0
Op Code No of bits: 6
Bit 31
Rs 5
Rt 5
Rd 5
Sh Amt 5
Fn Code 6
Register addresses are 5 bits (since 25=32). Example: If Rt is (i 32) E l i 0 1001 [09], then Rt = $9 = $t1.
The function code is the register-register function to it it f ti t be performed. Example: Function code 10 0000 [0x20] means add.
N. B. Dodge 09/09
00 0000
Bit 31
0 1000
0 1001
0 1010
0 0000
10 0000
Rt = $9 = $t1
Rd = $10 = $t2 The function code* for add is 10 0000, or 0x 20 (this is technically add with overflow).
The assembly language acronym for this instruction is: add $t2, $t0 $t1 i i i dd $t2 $t0, $t1.
* A list of op/function codes can be found in the P&H inside front cover or back cover. (Note that if you have an older hard-cover b k with codes on th i id b k cover, th are h d book ith d the inside back they shown in decimal form).
12 Lecture #11 Registers, System Calls, and More Instructions
N. B. Dodge 09/09
Exercise 1
00 0000 1 1001 0 1110 0 1111 0 0000 10 0100
00 0000
0 0111
1 0011
0 1000
0 0000
01 1000
13
N. B. Dodge 09/09
Register-to-Register Instructions
A review of R-R instructions studied in the last lecture:
14
add $s1, $t2, $t6: [$t2] + [$t6] [$s1]. addi $s1, $t2, 27: [$t2] + 27 [$s1]. sub $t3, $t4, $t5: [$t4] [$t5] [$t3]. Can also be immediate. mul $t0, $t1, $t2: [$t1] [$t2] [$t0]. div $t0, $t2, $t1: [$t2]/[$t1], quotient [LO] [$t0]. and $t0 $t1 $t2 [$t1] [$t2] [$t0] Al OR NOT NOR XOR d $t0, $t1, $t2: [$t1] [$t0]. Also OR, NOT, NOR, XOR. neg $t2, $s5: ([$s5]+1) [$t2]. abs $t1, $t0: |[$t0]| [$t1]. lui $t1, 23: 23 (0000 0000 0001 0111) upper 16 bits of $t1. $t1 $t1 li $t1, 78645329: 78645329 $t1 (pseudo; uses $at). move $s5, $t3: [$t3] [$s5] rem $t0, $ , $ $ , $t1, $t2: [$ ] [$ ], remainder [HI] [$ ] [$t1]/[$t2], [ ] [$t0].
N. B. Dodge 09/09
Note: In R-R instructions, the source register contents are NOT changed. Lecture #11 Registers, System Calls, and More Instructions
Miscellaneous Instructions
The following instructions are convenient to mention at this point:
nop: No operation. The computer does nothing for one instruction cycle. The set instructions: These instructions (which are ( technically register-register instructions) generally put a 1 or 0 into a destination register, depending on the comparative values of two other registers. We will cover these later in the lecture on branch instructions, as they are decision-making (or more properly, decision-support) instructions. Shift, and rotate instructions (also R-R instructions) will also be covered in that lecture.
15 Lecture #11 Registers, System Calls, and More Instructions
N. B. Dodge 09/09
System Calls
System calls allow our simulated MIPS to communicate to the user and perform a few other functions functions. Most system calls either initiate a print sequence to the system console, or read data from the keyboard. Syscall 10 halts the program and computer simulation. Certain system calls use specific registers (primarily $v0 and $ 0) Th system calls will not execute unless th d $a0). The t ll ill t t l the designated registers contain the correct information. y System calls are the user interface in SPIM.
16 Lecture #11 Registers, System Calls, and More Instructions
N. B. Dodge 09/09
Sometimes $a0 or $a1 are used in syscalls To print a number it is syscalls. number, loaded into $a0, a 1 is put into $v0, and the syscall is executed:
syscall 1 syscall 1
li $a0, 0x ab67 (this might also be loaded from memory) li $v0, 1 $ 0 syscall
More examples of syscalls are on the next slide. We will cover syscalls s scalls 5 and 8 later in detail. detail
Lecture #11 Registers, System Calls, and More Instructions
N. B. Dodge 09/09
18
In syscall 4, $a0 is loaded with the memory address of the first byte in the ASCII string to be output output. Characters are output until a null (character = 0) is encountered. Syscall 11 prints out the lower 8 bits of $a0 as an ASCII character. In syscall 12, the 8 bits representing the ASCII character input from the keyboard the lowest 8 bits of $v0 (erasing the 12!).
N. B. Dodge 09/09
Directives
Directives give the SPIM assembler program information. The main ones of interest are shown here:*
.text -- Program instructions follow text Program follow. .data -- Data required by the program follows. .word [number] -- The data element that follows is a 32-bit number. Note: Can be used to define several elements at once. .space [number] -- Reserve the number of bytes in memory shown and clear them to 0. .asciiz [character string] The following characters should be stored in g g memory as a byte string and null terminated. .ascii [character string] Same as .asciiz but no null termination. .frame -- Reserve the frame as shown on the stack.
* See Pervin appendix D for full list of directives. All directives are preceded by a period. 20 Lecture #11 Registers, System Calls, and More Instructions
N. B. Dodge 09/09
Labels
Labels notify the program to attach a name to a data declaration or to a text (program) line. Labels allow easy reference to a specific place in the program or to an argument that is required in the program.
Each data entry must be labeled with a unique name, or label. The specific name main: MUST label the first line of text in a PCSPIM p g program. Labels are always followed by a colon (:).
Examples:
string: .asciiz Hello, world -- Characters are named string. Hello, world string. adder: add $t1,$t2,$t3 This instruction is named adder.* calc: mul $t5,$t5,$t6 -- This instruction is named calc.
* Note: Do NOT use an instruction acronym as a label. The assembler will reject it.
21
N. B. Dodge 09/09
Comments
Comments are VERY useful in describing what a program is doing. You may not think you need them, but in general, comments are crucial to (a) remind you what you did later on, and (b) let OTHERS know how the program works. k Comments are always preceded by a pound sign. The assembler ignores a line (or part of a line) after a #. Comment lines do not wrap A two or more line comment must have a # wrap. sign at the beginning of each line. A comment may be appended on any instruction or data line. Simply enter a # and then the comment. Examples: p add $t1,$t1,$t6 # Computing the value K. mul $s1,$t1,$t1 # Calculating the area of the square. temp: .word 212 p # Defining temperature constant. g p .main: and $t1,$t7,$t8 # Start program with logical AND.
Lecture #11 Registers, System Calls, and More Instructions
N. B. Dodge 09/09
22
Data:
Starts with the .data directive. Data declarations covered later. Each data statement must include a definition and a label. Data may be listed before or after text.
23
N. B. Dodge 09/09
Example Program
The example program on the following page demonstrates instructions which were covered today and last lecture. First we will examine the program listing to see the instructions and to note the terminology associated with the program, much as we did in the last lecture. Then we will see the program in execution, to get a feel for the properties of the SPIM simulator on the PC. Two notes:
We will be using PCSPIM, the MIPS assembler/simulator for the PC. SPIM versions also exist for Mac and Unix systems (XSPIM). There are differences between PCSPIM and XSPIM, many of which are minor. However, note that the program examples given are in PCSPIM. Program start is labeled differently in XSPIM.
24 Lecture #11 Registers, System Calls, and More Instructions
N. B. Dodge 09/09
## ## ## ##
File lecture 11 demo program 1 Prints out answer to the question: Calculate 5*X2-3, where x=7 Output is: "answer = 242" nop li $t0, 7 move $a0, $t0 mul $a0, $a0, $t0 mul $a1, $a0, 5 , , sub $a2, $a1, 3 la $a0, ans li $v0, 4 syscall move $a0, $a2 li $v0, 1 syscall li $v0,10 syscall
Comments that name and describe i the program. Text directive: What follows is the program. main: label This is the
.text main: i
# load x (7) in $t0 start of the program. # Copy x (7) to Reg. a0 # Form X squared (7-squared or 7x7) # Form 5X squared ( q (5x7x7) ) # Form (5X squared)-3 ([5x7x7]-3) # Put string address into a0 # Load print string syscall code # Output "answer = " # Put answer in a0 (number printed out) # Load print integer syscall code # Print integer in a0 # "Done" -- i.e., stop
The program.
Data directive: What follows is the program data. Program data, properly labeled. Note label followed by a colon. g ,p p y y Closing comment.
Exercise 2
Write the following program as an exercise:
Label the ASCII character group hello world (cr/lf) as str:. Use the U th appropriate syscall to output th string t th console. i t ll t t t the t i to the l Use the appropriate syscall to stop the program. Remember to use the directives for program and data. Remember to label the start of your program properly.
26
N. B. Dodge 09/09
Homework
Write down two or three important things you learned today and add to your list list. Write down two or three things you did not clearly understand. After finishing the assigned reading, if you still have questions, see me during office hours. Readings, per syllabus. ( g project). Continue homework #4 (design p j )
29 Lecture #11 Registers, System Calls, and More Instructions
N. B. Dodge 09/09
MIPS INTEGER and GENERAL INSTRUCTIONS Instruction * abs add addu addi addiu and/andi *b beq * beqz * bge b bgez bgt bgtz ble blez * blt bltz Arg1 rd rd rd rt rt rd label rs rs rs rs rs rs rs rs rs rs Arg2 rs rs rs rs rs rs rt label rt t label rt label rt label rt label Arg3 rt rt imm imm rt label label l b l label label label Description put the absolute value of rs into rd rd = rs + rt (with overflow) rd = rs + rt (without overflow) rt = rs + imm (with overflow) rt = rs + imm (without overflow) put rs AND rt into rd (imm, (imm not rt in andi) branch to label branch to label if (rs==rt) branch to label if (rs==0) branch t l b l if (rs>=rt) b h to label ( t) branch to label if (rs>=0) branch to label if (rs>rt) branch to label if (rs>0) branch to label if (rs<=rt) ( ) branch to label if (rs<=0) branch to label if (rs<rt) branch to label if (rs<0)
* Indicates a pseudoinstruction (assembler generates more than one instruction to produce the same result; note that the pseudoinstruction has no opcode).
MIPS INTEGER and GENERAL INSTRUCTIONS (Continued) bne * bnez div j jal jalr jr la lb lbu lb * li lui lw lw * move * mul * neg nop nor * not or ori rs rs rd label [rd] [rd] rs rd rt rt rd rt rd rd rd rd rd rd rd rd rt rt label rs label rs label address address dd number number address offset(base) ( ) rs rs rt rs rs rs rs rs rt rt imm label rt branch to label if (rsrt) branch to label if (rs0) rd = rs DIV rt jump to label jump to label; save next instruction in rd jump to instruction at (rs), save next in rd jump to instruction at (rs) load address of word at label into rd load byte at address into rt, sign xtnd load byte at address i l db dd into rt load number into rd upper halfword of rt = 16-bit number load the word at address into rd load word at addr offset+base into rd move rs to rd rd = rs rt rd = rs do nothing rd = rs NOR rt rd = bitwise logical negation of rs rd = rs OR rt rt = rs OR imm
* Indicates a pseudoinstruction (assembler generates more than one instruction to produce the same result; note that the pseudoinstruction has no opcode).
MIPS INTEGER and GENERAL INSTRUCTIONS (Concluded) * rem * rol(ror) sb * seq q * sge * sgt * sle sll slt * sne sra srl sub sw sw syscall xor rd rd rt rd rd rd rd rd rd rd rd rd rd rt rt rd rs rs address rs rs rs rs rt rs rs rt rt rt ra rt rt rt rt sa rt rt sa sa rd = rs MOD rt rd = rs rotated left(right) by ra store byte in rt to address if (rs==rt) rd=1;else rd=0 ( ) ; if (rs>=rt) rd=1;else rd=0 if (rs>rt) rd=1;else rd=0 if (rs<=rt) rd=1;else rd=0 rd = rt shifted left by distance sa if (rs<rt) rd=1;else rd=0 if (rs!=rt) rd=1;else rd=0 rd = rt shifted right by sa, sign-extended rd = rt shifted right by sa, sa 0-extended rd = rs - rt store the word in rt to address store word in rt to addr offset+base do a system call depending on contents of $v0 rd = rs XOR rt
rs rt address offset(base) rs rt
* Indicates a pseudoinstruction (assembler generates more than one instruction to produce the same result; note that the pseudoinstruction has no opcode). Note: The above list is not complete, but contains all instructions of interest in EE 2310.
Supplementary Material pp y
The following material will not be covered in the lecture. R d and study i on your own, and you will Read d d it d ill have the opportunity (last slide) to earn a homework bonus. bonus
33
N. B. Dodge 09/09
34
Memory: M
+ Simplest possible CPU design. Very slow (was faster once upon a time, when memories were lots faster than CPUs) CPUs).
38
N. B. Dodge 09/09
39
N. B. Dodge 09/09
40
The MIPS, our target processor/assembly language to study is based on this victory of this ISA. The MIPS processor employs a RISC load/store ISA. ISA
42 Lecture #11 Registers, System Calls, and More Instructions
N. B. Dodge 09/09
Bonus Questions
Name ______________________ EE 2310 Class Section __________________ 1. 2. 3. 4. 5. 6. 6 7. 8.
44
Provide a one-sentence definition of a computer ISA. (T___/F___) Fixed/floating point choice is not an important ISA condition. (T___/F___) The RISC architecture allows mathematical/logical operations directly on arguments in memory memory. (T___/F___) The old-fashioned accumulator architecture is very complex. (T___/F___) Computer memory is much faster than the CPU. (T___/F___) (T /F ) Load and branch instructions combined occur much less often, often on average, than a combination of add, and, sub, and move instructions. What does RISC stand for? (T___/F___) The fastest architecture is the accumulator architecture.
Lecture #11 Registers, System Calls, and More Instructions
N. B. Dodge 09/09