Download presentation
Presentation is loading. Please wait.
Published byJodie Thornton Modified over 9 years ago
1
9/29: Lecture Topics Conditional branch instructions
Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding
2
Mailing List and Computer Labs
CSE410 mailing list Subscribe if you weren’t automatically or you use a different account Send to with ‘subscribe cse410’ in the body Computer accounts MSCC Lab in basement of Communications building Username = passwd =
3
Conditional Branch A change of the flow of control of the program that depends on a condition ... ... ? ... yes no ...
4
Control flow in C Conditional branch constructs:
while, do while loops for loops Unconditional jump constructs: goto switch statements
5
Branching Instructions
beq bne other real instructions: b, bgez, bgtz, more... other pseudoinstructions: beqz, bge, bgt, ble, blt, more...
6
Pseudoinstructions One-to-one mapping between assembly and machine language not strictly true Assembler can do some of the work for you Example: slt is a real instruction; blt is a pseudoinstruction move is a pseudoinstruction
7
Labels in MIPS MIPS allows text tags
a name for a place to branch to Under the covers, the assembler converts the text tag into an address loop: add $t0, $t0, $t0 # bne $t0, $t1, loop # sw $t2, 0($t3) #
8
Building a while loop in MIPS
Idea: translate i=0; while(i<100) { i++; } into assembly.
9
How about a for loop? One answer: translate from for to while, then use while loop conventions This is typically how compilers handle loops i=0; while(i<N) { do_stuff(i); i++; } for(i=0; i<N; i++) { do_stuff(i); }
10
Example C Program int array[100]; void main() { int i;
for(i=0; i<100; i++) array[i] = i; }
11
An Assembly Version main: move $t0, $0 # la $t1, array #
start: bge $t0, 100, exit # sw $t0, 0($t1) # addi $t0, $t0, 1 # addi $t1, $t1, 4 # j start # exit: j $ra #
12
Unconditional Jump (Mostly) the same as branch
No choice about it; just go to the label How are branch and jump different? we’ll see when we get to instruction encoding
13
Binary Numbers Instead of 0-9 we can only use 0 and 1
Also known as base-2 Counting to 10ten in binary 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010 Binary arithmetic 1 1011 +1010 10101
14
Binary -> Decimal Converting from Base-2 to Base-10 12 = 20 = 110
102 = 21 = 210 1002 = 22 = 410 10002 = 23 = 810 11102 = 1*23 + 1*22 + 1*21 + 0*20 = = 1410 =
15
Decimal -> Binary Repeatedly divide by 2 until you reach 0
Reverse the order of the remainders 5010 50/2 = 25 remainder 0 25/2 = 12 remainder 1 12/2 = 6 remainder 0 6/2 = 3 remainder 0 3/2 = 1 remainder 1 1/2 = 0 remainder 1 7510 75/2 = remainder /2 = remainder
16
Hexadecimal Numbers Instead of 0-9 we use 0-15
0-15 is confusing so we use 0-9, A-F Also known as base-16 Counting to 18ten in hex 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, 11, 12 Hex arithmetic 1 1 2E97 +A2B3 D14A A3AD38B993CA93C930B -A3AD38B993CA93C930A
17
Hexadecimal <-> Binary
Each four binary digits represents one hexadecimal digit Can quickly convert either way 0x2A07 = = 00002 = 016 00012 = 116 00102 = 216 00112 = 316 01002 = 416 01012 = 516 01102 = 616 01112 = 716 10002 = 816 10012 = 916 10102 = A16 10112 = B16 11002 = C16 11012 = D16 11102 = E16 11112 = F16
18
Hexadecimal -> Decimal
Converting from Base-16 to Base-10 116 = 160 = 110 1016 = 161 = 1610 10016 = 162 = 25610 = 163 = A32 = A* *160 = 10* *160 = = 163
19
Decimal -> Hexadecimal
Repeatedly divide by 16 until you reach 0 Reverse the order of the remainders 50010 500/16 = 31 remainder 4 31/16 = 1 remainder 15 (F) 1/16 = 0 remainder 1 1F416
20
What you need to know Be able to convert to/from small decimal numbers from/to binary or hex Be able to/from convert any binary number from/to a hex number
21
Stored Program So far, we’ve seen that data comes from memory
It turns out that the program comes from memory also Each instruction in the program has an address Von Neumann computer (p. 33)
22
Program Layout (p. 160) Address Reserved 0x00400000
Reserved 0x Program instructions Text 0x Static data Global variables 0x Dynamic data and stack 0x7fffffff
23
The Text Segment Let’s zoom in on the text segment: 0x00400000 Text
add $t0, $t1, $t2 0x sub $t0, $t0, $t3 lw $s1, 4($t0) 0x
24
Jump Register Now we’re ready for the jr instruction Syntax:
Effect: jump to the instruction at the address in $t0 jr $t0
25
Another Look at Labels Labels are text tags
The assembler translates them into addresses and does search-and-replace loop: add $t0, $t0, $t0 bne $t0, $t1, loop sw $t2, 0($t3)
26
C Switch (p. 129) switch(k) { case(0): f = i+j; break; case(1):
f = g+h; break; case(2): f = g-h; break; case(3): f = i-j; break; }
27
Idea for implementing switch
Table of addresses code for k=0 1 code for k=1 2 3 code for k=2 code for k=3
28
Implementing C switch add $t1, $s5, $s5 add $t1, $t1, $t1
lw $t0, 0($t1) jr $t0 L0: add $s0, $s3, $s4 j Exit L1: add $s0, $s1, $s2 L2: sub $s0, $s1, $s2 L3: sub $s0, $s3, $s4 Exit:
29
Instruction Encoding How does the assembler translate each instruction into bits? add $t0, $s1, $s2 assembler
30
Fields Split the 32-bit instruction into fields:
op+funct: Which instruction? rs, rt: Register source operands rd: Register destination operand 32 bits op rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
31
Encoding an add instruction
Let’s translate: Opcode for add is (p. A-55) The funct for add is The code for $t0 is (p. A-23) The code for $s1 is 10001 The code for $s2 is 10010 add $t0, $s1, $s2
32
Instruction Formats The 32 bits can be split up more than one way
For add, it was This is called R-format For lw, we use instead This is called I-format 32 bits op rs rt address 6 bits 5 bits 5 bits 16 bits
33
Translating into I-format
lw $t0, 100($t1) The opcode for lw is The code for $t0 is 01000 The code for $t1 is 01001 Binary for 100 is
34
Branch vs. Jump Branch instructions use the I-format
The destination address is 16 bits You can only branch ±32KB away Jump instructions use another format called the J-format The destination address is 26 bits You can jump up to ±32MB away What’s the tradeoff?
35
What to Take Away I don’t care if you: I do care that you:
Know how many bits are in each field Know what the opcodes are Know what the codes for the registers are I do care that you: Know what fields are Know why we need more than one instruction format
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.