1 9/29: lecture topics conditional branch instructions unconditional jump instructions...
TRANSCRIPT
![Page 1: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/1.jpg)
1
9/29: Lecture Topics
• Conditional branch instructions• Unconditional jump instructions• Hexadecimal/Binary/Decimal• Instruction encoding
![Page 2: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/2.jpg)
2
Mailing List and Computer Labs
• CSE410 mailing list– Subscribe if you weren’t automatically
or you use a different account• Send email to
[email protected] with ‘subscribe cse410’ in the body
• Computer accounts– MSCC Lab in basement of
Communications building– Username = passwd =
![Page 3: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/3.jpg)
3
Conditional Branch
yes
no
?
...
...
...
...
A change of the flow of control
of the program that depends on
a condition
![Page 4: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/4.jpg)
4
Control flow in C
• Conditional branch constructs:– while, do while loops– for loops
• Unconditional jump constructs:– goto– switch statements
![Page 5: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/5.jpg)
5
Branching Instructions
• beq• bne• other real instructions: b, bgez, bgtz, more...
• other pseudoinstructions: beqz, bge, bgt, ble, blt, more...
![Page 6: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/6.jpg)
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
![Page 7: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/7.jpg)
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) #
![Page 8: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/8.jpg)
8
Building a while loop in MIPS
i=0;while(i<100) { i++;}
Idea: translate
into assembly.
![Page 9: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/9.jpg)
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
for(i=0; i<N; i++) { do_stuff(i);}
i=0;while(i<N) { do_stuff(i); i++;}
![Page 10: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/10.jpg)
10
Example C Program
int array[100];
void main() {
int i;
for(i=0; i<100; i++)
array[i] = i;
}
![Page 11: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/11.jpg)
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 #
![Page 12: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/12.jpg)
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
![Page 13: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/13.jpg)
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+101010101
11001001- 1010110
![Page 14: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/14.jpg)
14
Binary -> Decimal
• Converting from Base-2 to Base-1012 = 20 = 110
102 = 21 = 210
1002 = 22 = 410
10002 = 23 = 810
11102 = 1*23 + 1*22 + 1*21 + 0*20
= 8+4+2+0 = 1410
1010112 =
![Page 15: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/15.jpg)
15
Decimal -> Binary
• Repeatedly divide by 2 until you reach 0
• Reverse the order of the remainders5010
50/2 = 25 remainder 025/2 = 12 remainder 112/2 = 6 remainder 0 6/2 = 3 remainder 0 3/2 = 1 remainder 1 1/2 = 0 remainder 1
1100102
7510
75/2 = remainder /2 = remainder /2 = remainder /2 = remainder /2 = remainder /2 = remainder /2 = remainder
![Page 16: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/16.jpg)
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
![Page 17: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/17.jpg)
17
Hexadecimal <-> Binary
• Each four binary digits represents one hexadecimal digit
• Can quickly convert either way– 0x2A07 = 0010 1010 0000 0111– 1111 0011 0101 1011 =
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
![Page 18: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/18.jpg)
18
Hexadecimal -> Decimal
• Converting from Base-16 to Base-10116 = 160 = 110
1016 = 161 = 1610
10016 = 162 = 25610
100016 = 163 = 153610
A32 = A*161 + 3*160
= 10*161 + 3*160 = 160 + 3 = 163
![Page 19: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/19.jpg)
19
Decimal -> Hexadecimal
• Repeatedly divide by 16 until you reach 0
• Reverse the order of the remainders50010
500/16 = 31 remainder 4 31/16 = 1 remainder 15 (F) 1/16 = 0 remainder 1
1F416
![Page 20: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/20.jpg)
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
![Page 21: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/21.jpg)
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)
![Page 22: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/22.jpg)
22
Program Layout (p. 160)
Address0
0x00400000
0x10000000
0x10008000
0x7fffffff
Reserved
Text
Static data
Dynamic data and stack
Program instructions
Global variables
![Page 23: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/23.jpg)
23
The Text Segment
• Let’s zoom in on the text segment:
0x00400000
0x10000000
Text
0x004000000x004000040x00400008
add $t0, $t1, $t2
sub $t0, $t0, $t3
lw $s1, 4($t0)
![Page 24: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/24.jpg)
24
Jump Register
• Now we’re ready for the jr instruction
• Syntax:
• Effect: jump to the instruction at the address in $t0
jr $t0
![Page 25: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/25.jpg)
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)
![Page 26: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/26.jpg)
29
Instruction Encoding
• How does the assembler translate each instruction into bits?
add $t0, $s1, $s2
00000010001100100100000000100000
assembler
![Page 27: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/27.jpg)
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
![Page 28: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/28.jpg)
31
• Let’s translate:
• Opcode for add is 000000 (p. A-55)• The funct for add is 100000• The code for $t0 is 01000 (p. A-23)• The code for $s1 is 10001• The code for $s2 is 10010
Encoding an add instruction
add $t0, $s1, $s2
![Page 29: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/29.jpg)
32
Instruction Formats
• The 32 bits can be split up more than one way
• For add, it was 6-5-5-5-5-6– This is called R-format
• For lw, we use 6-5-5-16 instead– This is called I-format
32 bits
op rs rt address
6 bits 5 bits 5 bits 16 bits
![Page 30: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/30.jpg)
33
Translating into I-format
• The opcode for lw is 100011• The code for $t0 is 01000• The code for $t1 is 01001• Binary for 100 is
lw $t0, 100($t1)
![Page 31: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/31.jpg)
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?
![Page 32: 1 9/29: Lecture Topics Conditional branch instructions Unconditional jump instructions Hexadecimal/Binary/Decimal Instruction encoding](https://reader036.vdocuments.mx/reader036/viewer/2022062305/5697bfb91a28abf838c9fb22/html5/thumbnails/32.jpg)
35
What to Take Away
• I don’t care if 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