Upload
jodie-thornton
View
225
Download
0
Embed Size (px)
Citation preview
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 email to
[email protected] with ‘subscribe cse410’ in the body
• Computer accounts– MSCC Lab in basement of
Communications building– Username = passwd =
3
Conditional Branch
yes
no
?
...
...
...
...
A change of the flow of control
of the program that depends on
a condition
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
i=0;while(i<100) { i++;}
Idea: translate
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
for(i=0; i<N; i++) { do_stuff(i);}
i=0;while(i<N) { do_stuff(i); 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+101010101
11001001- 1010110
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 =
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
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 = 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
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
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
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)
Address0
0x00400000
0x10000000
0x10008000
0x7fffffff
Reserved
Text
Static data
Dynamic data and stack
Program instructions
Global variables
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)
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)
29
Instruction Encoding
• How does the assembler translate each instruction into bits?
add $t0, $s1, $s2
00000010001100100100000000100000
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
• 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
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
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)
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:– 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