COMP-273 (1)
Slides from Patterson’s 61C
COMP-273 Instruction Representation 2
Copyright By PowCoder代写 加微信 powcoder
°MIPS defines instructions to be same size as data (one word) so that they can use the same memory (can use lw and sw).
°Machine Language Instruction: 32 bits representing a single instruction
COMP-273 (2)
Slides from Patterson’s 61C
°Computer actually stores programs as a series of these machine intructions.
COMP-273 (3)
Slides from Patterson’s 61C
°Branch instruction encoding °Jump instructions °Disassembly
°Pseudoinstructions and
“True” Assembly Language (TAL) v. “MIPS” Assembly Language (MAL)
Branches: PC-Relative Addressing (1/5)
°Use I-Format
°opcode specifies beq v. bne
°Rs and Rt specify registers to compare
°What can immediate specify? •Immediate is only 16 bits
• PC is 32-bit pointer to memory
• So immediate cannot specify entire address to branch to.
COMP-273 (4)
Slides from Patterson’s 61C
Branches: PC-Relative Addressing (2/5)
°How do we usually use branches?
• Answer: if-else, while, for
• Loops are generally small: typically up to 50 instructions
• Function calls and unconditional jumps are done using jump instructions (j and jal), not the branches.
°Conclusion: Though we may want to branch to anywhere in memory, a single
branch will generally change the PC by a very small amount.
COMP-273 (5)
Slides from Patterson’s 61C
Branches: PC-Relative Addressing (3/5)
°Solution: PC-Relative Addressing
°Let the 16-bit immediate field be a signed two’s complement integer to be added to the PC if we take the branch.
°Now we can branch +/- 215 bytes from the PC, which should be enough to cover any loop.
°Any ideas to further optimize this?
COMP-273 (6)
Slides from Patterson’s 61C
Branches: PC-Relative Addressing (4/5)
°Note: Instructions are words, so they’re word aligned (byte address is always a multiple of 4, which means it ends with 00 in binary).
• So the number of bytes to add to the PC will always be a multiple of 4.
• So specify the immediate in words.
°Now, we can branch +/- 215 words from the PC (or +/- 217 bytes), so we can handle loops 4 times as large.
COMP-273 (7)
Slides from Patterson’s 61C
Branches: PC-Relative Addressing (5/5)
°Branch Calculation:
• If we don’t take the branch:
PC = PC + 4
PC+4 = byte address of next instruction
• If we do take the branch:
PC = (PC + 4) + (immediate* 4)
• Observations
– Immediate field specifies the number of words to jump, which is simply the number of instructions to jump.
– Immediate field can be positive or negative. – Due to hardware, add immediate to (PC+4),
not to PC; will be clearer why later in course
COMP-273 (8)
Slides from Patterson’s 61C
Branch Example (1/3)
°MIPS Code:
Loop: beq $9,$0,End add $8,$8,$10
addi $9,$9,-1
j Loop End:
°Branch is I-Format: opcode = 4 (look up in table) rs = 9 (first operand)
rt = 0 (second operand)
COMP-273 (9) immediate = ???
Slides from Patterson’s 61C
Branch Example (2/3)
°MIPS Code:
Loop: beq $9,$0,End addi $8,$8,$10
addi $9,$9,-1 End: j Loop
°Immediate Field:
• Number of instructions to add to (or subtract from) the PC, starting at the instruction following the branch.
• In beq case, immediate = 3
COMP-273 (10)
Slides from Patterson’s 61C
Branch Example (3/3)
°MIPS Code:
Loop: beq $9,$0,End addi $8,$8,$10 addi $9,$9,-1
j Loop End:
decimal representation: binary representation:
0000000000000011
COMP-273 (11)
Slides from Patterson’s 61C
Questions on PC-addressing
COMP-273 (12)
Slides from Patterson’s 61C
°Does the value in branch field change if we move the code?
°What do we do if its > 2^15 instructions?
°Since its limited to +- 2^15 instructions, doesn’t this generate lots of extra MIPS instructions?
°Why do we need all these addressing modes? Why not just one?
J-Format Instructions (1/5)
°For branches, we assumed that we won’t want to branch too far, so we can specify change in PC.
°For general jumps (j and jal), we may jump to anywhere in memory.
°Ideally, we could specify a 32-bit memory address to jump to.
°Unfortunately, we can’t fit both a 6-bit opcode and a 32-bit address into a single 32-bit word, so we compromise.
COMP-273 (13)
Slides from Patterson’s 61C
J-Format Instructions (2/5)
°Define “fields” of the following number of bits each:
°As usual, each field has a name: °Key Concepts
• Keep opcode field identical to R-format and I-format for consistency.
• Combine all other fields to make room for large target address.
target address
COMP-273 (14)
Slides from Patterson’s 61C
J-Format Instructions (3/5)
°For now, we can specify 26 bits of the 32-bit bit address.
°Optimization:
• Note that, just like with branches, jumps will only jump to word aligned addresses, so last two bits are always 00 (in binary).
• So let’s just take this for granted and not even specify them.
COMP-273 (15)
Slides from Patterson’s 61C
J-Format Instructions (4/5)
°So, we can specify 28 bits of the 32-bit address.
°Where do we get the other 4 bits?
• By definition, take the 4 highest order bits
from the PC.
• Technically, this means that we cannot
jump to anywhere in memory, but it’s adequate 99.9999…% of the time, since
programs aren’t that long.
• If we absolutely need to specify a 32-bit address, we can always put it in a register and use the jr instruction.
COMP-273 (16)
Slides from Patterson’s 61C
J-Format Instructions (5/5)
• C = PC[31..28]
|| target address (26 bits)
• Note: II means concatenation
4 bits || 26 bits || 2 bits = 32-bit address
°Understand where each part came from!
COMP-273 (17)
Slides from Patterson’s 61C
COMP-273 (18)
Slides from Patterson’s 61C
°Branch instruction encoding °Jump instructions °Disassembly
°Pseudoinstructions and
“True” Assembly Language (TAL) v. “MIPS” Assembly Language (MAL)
Decoding Machine Language
°How do we convert 1s and 0s to C code?
Machine language => C °For each 32 bits:
• Look at opcode: 0 means R-Format, 2 or 3 mean J-Format, otherwise I-Format.
• Use instruction type to determine which fields exist.
• Write out MIPS assembly code, converting each field to name, register number/name, or decimal/hex number.
• Logically convert this MIPS code into valid
COMP-273 (19)
Slides from Patterson’s 61C
C code. Always possible? Unique?
Decoding Example (1/7)
°Here are six machine language instructions in hex:
°Let the first instruction be at address 4,194,30410 (0x00400000).
°Next step: convert to binary COMP-273 (20)
Slides from Patterson’s 61C
Decoding Example (2/7)
°The six machine language instructions in binary:
00000000000000000001000000100101 00000000000001010100000000101010
00010001000000000000000000000011 00000000010001000001000000100000 00100000101001011111111111111111 00001000000100000000000000000001
°Next step: identify opcode and format
COMP-273 (21)
Slides from Patterson’s 61C
Decoding Example (3/7)
°Select the opcode (first 6 bits) to determine the format:
R 00000000000000000001000000100101 R 00000000000001010100000000101010 I 00010001000000000000000000000011 R 00000000010001000001000000100000 I 00100000101001011111111111111111
00001000000100000000000000000001
°Look at opcode: 0 means R-Format,
2 or 3 mean J-Format, otherwise I-Format.
° Next step: separation of fields COMP-273 (22)
Slides from Patterson’s 61C
Decoding Example (4/7)
°Fields separated based on format/opcode: Format:
R R I R I J
COMP-273 (23)
Slides from Patterson’s 61C
°Next step: translate (“disassemble”) to MIPS assembly instructions
Decoding Example (5/7)
°MIPS Assembly (Part 1):
0x00400000 or $2,$0,$0
0x00400004 slt $8,$0,$5
0x00400008 beq $8,$0,3
0x0040000c add $2,$2,$4
0x00400010 addi $5,$5,-1
0x00400014 j 0x100001
°Better solution: translate to more meaningful instructions (fix the branch/jump and add labels)
COMP-273 (24)
Slides from Patterson’s 61C
Decoding Example (6/7)
°MIPS Assembly (Part 2): or $v0,$0,$0
Loop: slt $t0,$0,$a1 beq $t0,$0,Exit add $v0,$v0,$a0
addi $a1,$a1,-1
°Next step: translate to C code (be creative!)
COMP-273 (25)
Slides from Patterson’s 61C
Decoding Example (7/7)
• Mapping: $v0: product $a0: multiplicand
$a1: multiplier
product = 0;
while (multiplier > 0) {
product += multiplicand;
multiplier -= 1;
COMP-273 (26)
Slides from Patterson’s 61C
COMP-273 (27)
Slides from Patterson’s 61C
°Branch instruction encoding °Jump instructions °Disassembly
°Pseudoinstructions and
“True” Assembly Language (TAL) v. “MIPS” Assembly Language (MAL)
Review from Last Lecture: lui
°So how does lui help us?
• Example:
addi $t0,$t0, 0xABABCDCD
lui $at, 0xABAB
ori $at, $at, 0xCDCD add $t0,$t0,$at
• Now each I-format instruction has only a 16- bit immediate.
°Wouldn’t it be nice if the assembler would this for us automatically?
– If number too big, then just automatically replace addi with lui, ori, add
COMP-273 (28)
Slides from Patterson’s 61C
True Assembly Language
°Pseudoinstruction: A MIPS instruction that doesn’t turn directly into a machine language instruction.
°What happens with pseudoinstructions?
• They’re broken up by the assembler into several “real” MIPS instructions.
• But what is a “real” MIPS instruction? Answer in a few slides
°First some examples COMP-273 (29)
Slides from Patterson’s 61C
Example Pseudoinstructions
°Register Move
move reg2,reg1 Expands to:
add reg2,$zero,reg1
°Load Immediate
li reg,value
If value fits in 16 bits:
addi reg,$zero,value
lui reg,upper 16 bits of value ori reg,$zero,lower 16 bits
COMP-273 (30)
Slides from Patterson’s 61C
True Assembly Language
• When breaking up a pseudoinstruction, the assembler may need to use an extra register.
• If it uses any regular register, it’ll overwrite whatever the program has put into it.
°Solution:
• Reserve a register ($1, called $at for “assembler temporary”) that the assembler will use when breaking up pseudo- instructions.
• Since the assembler may use this at any time, it’s not safe to code with it.
COMP-273 (31)
Slides from Patterson’s 61C
Example Pseudoinstructions
°Rotate Right Instruction ror reg, value
Expands to:
srl $at, reg, value sll reg, reg, 32-value or reg, reg, $at
°No operation instruction nop
Expands to instruction = 0ten,
sll $0, $0, 0
COMP-273 (32)
Slides from Patterson’s 61C
Example Pseudoinstructions
°Wrong operation for operand
addu reg,reg,value#shouldbeaddiu
If value fits in 16 bits:
addiu reg,reg,value
lui $at,upper 16 bits of value ori $at,$zero,lower 16 bits addu reg,reg,$at
COMP-273 (33)
Slides from Patterson’s 61C
True Assembly Language
°MAL (MIPS Assembly Language): the set of instructions that a programmer may use to code in MIPS; this includes pseudoinstructions
°TAL (True Assembly Language): the set of instructions that can actually get
translated into a single machine language instruction (32-bit binary string)
°A program must be converted from MAL into TAL before it can be translated into 1s and 0s.
COMP-273 (34)
Slides from Patterson’s 61C
Questions on Pseudoinstructions
COMP-273 (35)
Slides from Patterson’s 61C
°How does MIPS recognize pseudoinstructions?
Peer Instruction
°Which of the codes below are pseudo-instructions (MIPS Assembly Language); that is, they are not TAL?
i. addi $t0, $t1, 40000
ii. beq $s0, 10, Exit
iii.sub $t0,$t1,1 A. i. only
B. ii. only
C. iii. only
D. i. and ii.
E. ii. and iii.
F. All of the above
COMP-273 (36)
Slides from Patterson’s 61C
Peer Instruction
°Which of the codes below are pseudo-instructions (MIPS Assembly Language); that is, they are not TAL?
i. addi $t0, $t1, 40000 40,000 > +32,767 =>lui,ori
ii. beq $s0, Exit
iii. sub $t0, $t1, 1 A. i. only
B. ii. only
C. iii. only
D. i. and ii.
E. ii. and iii.
F. All of the above
beq: both must be registers Sub: both must be registers;
even if it was subi,
there is no subi in TAL; generates addi $t0,$t1, -1
COMP-273 (37)
Slides from Patterson’s 61C
°Machine Language Instruction:
32 bits representing a single instruction
target address
COMP-273 (38)
Slides from Patterson’s 61C
°Branches use PC-relative addressing, Jumps use absolute addressing.
°Disassembly is simple and starts by decoding opcode field.
°Assembler expands real instruction set
(TAL) with pseudoinstructions (=>MAL)
Bonus slides
COMP-273 (39)
Slides from Patterson’s 61C
°The following slides are more practice on the differences between a pointer and a value, and showing how to use pointers
Assembly Code to Implement Pointers
COMP-273 (40)
Slides from Patterson’s 61C
°dereferencing Þ data transfer in asm.
… = … *p …; Þ load
(get value from location pointed to by p)
load word (lw) if int pointer,
load byte unsigned (lbu) if char pointer
*p = …; Þ store
(put value into location pointed to by p)
Assembly Code to Implement Pointers
c is int, has value 100, in memory at address 0x10000000, p in $a0, x in $s0
p = &c; /* p gets 0x10000000 */
x=*p; /*xgets100*/ *p = 200; /* c gets 200 */
# p = &c; /* p gets 0x10000000 */
lui $a0,0x1000 # p = 0x10000000
#x=*p; /*xgets100*/
lw $s0, 0($a0) # dereferencing p
# *p = 200; /* c gets 200 */
addi $t0,$0,200
sw $t0, 0($a0) # dereferencing p
COMP-273 (41)
Slides from Patterson’s 61C
Pointers to structures
°C Example – linked list:
struct node {
struct node *next;
int value;
If p is a pointer to a node, declared
with struct node *p, then: (*p).value or p->value for “value” field,
(*p).next or p->next for pointer to next node
COMP-273 (42)
Slides from Patterson’s 61C
COMP-273 (43)
Linked-list in C
main (void) {
struct node *head, *temp, *ptr; int sum;
ptr = ptr->next; }
Slides from Patterson’s 61C
/* create the nodes*/ head = (struct node *)
malloc(sizeof(struct node)); head->value = 23;
head->next = 0;
temp = (struct node *) malloc(sizeof(struct node));
temp->next = head; temp->value = 42;
head = temp;
/* add up the values */ ptr = head; sum = 0; while (ptr != 0) {
sum += ptr->value;
Linked-list in MIPS Assember (1/2)
# head:s0, temp:s1, ptr:s2, sum:s3
# create the nodes
li $a0,8 # sizeof(node) jal malloc # the call
move $s0,$v0 # head gets result li $t0,23
sw $t0,4($s0) # head->value = 23
sw $zero,0($s0)# head->next = NULL
jal malloc
move $s1,$v0 # temp = malloc
sw $s0,0($s1) # temp->next = head li $t0,42
sw $t0,4($s1) # temp->value = 42
move $s0,$s1 # head = temp
COMP-273 (44)
Slides from Patterson’s 61C
Linked-list in MIPS Assember (2/2)
# head:s0, temp:s1, ptr:s2, sum:s3
# add up the values
move $s2,$s0 # ptr = head move $s3,$zero # sum = 0
loop: beq $s2,$zero,exit # exit if done lw $t0,4($s2) # get value
addu $s3,$s3,$t0 # compute new sum lw $s3,0($s2) # ptr = ptr->next j loop # repeat
exit: done
COMP-273 (45)
Slides from Patterson’s 61C
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com