9 September 2004 Questions Programming Continued 992004 Comp
9 September 2004 • Questions? • Programming Continued 9/9/2004 Comp 120 Fall 2004 1
So far we’ve learned: • MIPS — loading words but addressing bytes — arithmetic on registers only • Instruction Meaning add $s 1, $s 2, $s 3 sub $s 1, $s 2, $s 3 lw $s 1, 100($s 2) Memory[$s 2+100] sw $s 1, 100($s 2) $s 1 = $s 2 + $s 3 $s 1 = $s 2 – $s 3 $s 1 = 9/9/2004 Memory[$s 2+100] = Comp 120 Fall 2004 2
Execution Example Program Counter 200 Registers (32 bits) 6 1234 7 23 8 120 9 -314159 10 Memory(32 bits) 200 1000110100001001 00000000 LW $9, 0($8) 112 8 204 0000000100100111 0100100000 ADD $9, $7 116 13 208 1010110100001001 0000001000 SW $9, 8($8) 120 21 124 34 128 55 132 89 212 Instruction Register (32 bits) R 316 I 9/9/2004 Memory(32 bits) op rs rt rd shft func 6 bits 5 bits 6 bits op rs rt offset 6 bits 5 bits 16 bits Comp 120 Fall 2004 3
Execution Example: Fetch(200) Program Counter 200 Registers 6 1234 7 23 8 120 9 -314159 10 Memory 200 1000110100001001 00000000 LW $9, 0($8) 112 8 204 0000000100100111 0100100000 ADD $9, $7 116 13 208 1010110100001001 0000001000 SW $9, 8($8) 120 21 124 34 128 55 132 89 212 Instruction Register R 316 I 9/9/2004 100011 01000 01001 35 8 9 Comp 120 Fall 2004 00000000 0 4
Execution Example: Execute(200) Program Counter 204 Registers 6 1234 7 23 8 120 9 21 10 316 Memory 200 1000110100001001 00000000 LW $9, 0($8) 112 8 204 0000000100100111 0100100000 ADD $9, $7 116 13 208 1010110100001001 0000001000 SW $9, 8($8) 120 21 124 34 128 55 132 89 212 Instruction Register R I 9/9/2004 Memory 100011 01000 01001 35 8 9 Comp 120 Fall 2004 00000000 0 5
Execution Example: Fetch(204) Program Counter 204 Registers 6 1234 7 23 8 120 9 21 10 316 Memory 200 1000110100001001 00000000 LW $9, 0($8) 112 8 204 0000000100100111 0100100000 ADD $9, $7 116 13 208 1010110100001001 0000001000 SW $9, 8($8) 120 21 124 34 128 55 132 89 212 Instruction Register 000000 R 01001 00111 01001 0 9 7 9 00000 100000 0 32 I 9/9/2004 Comp 120 Fall 2004 6
Execution Example: Execute(204) Program Counter 208 Registers 6 1234 7 23 8 120 9 44 10 316 Memory 200 1000110100001001 00000000 LW $9, 0($8) 112 8 204 0000000100100111 0100100000 ADD $9, $7 116 13 208 1010110100001001 0000001000 SW $9, 8($8) 120 21 124 34 128 55 132 89 212 Instruction Register 000000 R 01001 00111 01001 0 9 7 9 00000 100000 0 32 I 9/9/2004 Comp 120 Fall 2004 7
Execution Example: Fetch(208) Program Counter 208 Registers 6 1234 7 23 8 120 9 44 10 316 Memory 200 1000110100001001 00000000 LW $9, 0($8) 112 8 204 0000000100100111 0100100000 ADD $9, $7 116 13 208 1010110100001001 0000001000 SW $9, 8($8) 120 21 124 34 128 55 132 89 212 Instruction Register R I 9/9/2004 Memory 101011 01000 01001 43 8 9 Comp 120 Fall 2004 0000001000 8 8
Execution Example: Execute(208) Program Counter 212 Registers 6 1234 7 23 8 120 9 44 10 316 Memory 200 1000110100001001 00000000 LW $9, 0($8) 112 8 204 0000000100100111 0100100000 ADD $9, $7 116 13 208 1010110100001001 0000001000 SW $9, 8($8) 120 21 124 34 128 44 132 89 212 Instruction Register R I 9/9/2004 Memory 101011 01000 01001 43 8 9 Comp 120 Fall 2004 0000001000 8 9
Control • Decision making instructions – alter the control flow, – change the "next" instruction to be executed by changing the PC • MIPS conditional branch instructions: bne $t 0, $t 1, Label beq $t 0, $t 1, Label • Example: if (i==j) h = i + j; bne $s 0, $s 1, Label add $s 3, $s 0, $s 1 Label: . . 9/9/2004 Comp 120 Fall 2004 10
Control • MIPS unconditional branch instructions: j label jal label # function call jr $ra # function return • Example: if (i!=j) h=i+j; else h=i-j; 9/9/2004 beq $s 4, $s 5, Lab 1 add $s 3, $s 4, $s 5 j Lab 2 Lab 1: sub $s 3, $s 4, $s 5 Lab 2: . . . Comp 120 Fall 2004 11
Control Flow • • We have: beq, bne, what about Branch-if-less-than? New instruction: if $s 1 < $s 2 then $t 0 = 1 slt $t 0, $s 1, $s 2 else $t 0 = 0 • Can use this instruction to build "blt $s 1, $s 2, Label" — can now build general control structures Note that the assembler needs a register to do this, — there are policy of use conventions for registers • 9/9/2004 Comp 120 Fall 2004 12
So far: • Instruction Meaning add $s 1, $s 2, $s 3 lw $s 1, 100($s 2) bne $s 4, $s 5, L $s 5 j Label jal label addr jr $ra $s 1 = $s 2 + $s 3 $s 1 = Memory[$s 2+100] Next instr. is at Label if $s 4 != Next instr. is at Label Next instr at label, save return Next instr is addr in ra • R Formats: op rs rt rd I op rs rt 16 bit address J op 9/9/2004 shamt funct 26 bit address Comp 120 Fall 2004 13
Policy of Use Conventions 9/9/2004 Comp 120 Fall 2004 14
Stack Pointer? • Register $sp is used to keep track of the stack • Nothing special about $sp in the hardware, just a convention • Stack starts at the TOP of memory and grows DOWN (Why? ) • Allocate space on the stack by decrementing $sp • Free space on the stack by incrementing $sp • Used to save return address and temporary variables as well as for local variables and arrays 9/9/2004 Comp 120 Fall 2004 15
Memory Layout 9/9/2004 Comp 120 Fall 2004 16
Making code work! • • This example is available online. You should write algorithms in C/JAVA first – Then HAND translate… 9/9/2004 Comp 120 Fall 2004 17
C to Assembler char S 1[] = "This is a string. " int Foo[16]; char S 2[100]; void main() { strcpy(S 2, S 1); } void strcpy(char* dst, char* src) { int i = 0; while((dst[i] = src[i]) != 0) i = i+1; } 9/9/2004 Comp 120 Fall 2004 18
Data Segment #char S 1[] = "This is a string. " #int Foo[16]; #char S 2[100]; S 1: Foo: S 2: 9/9/2004 . data. asciiz. space 16. space 100 "This is a string. " # some junk # destination string Comp 120 Fall 2004 19
strcpy #void strcpy(char* dst, char* src) { # int i = 0; # while((dst[i] = src[i]) != 0) # i = i+1; # } strcpy: move $t 0, $zero L 1: add $t 1, $a 1, $t 0 lb $t 2, 0($t 1) add $t 1, $a 0, $t 0 sb $t 2, 0($t 1) addi $t 0, 1 bne $t 2, $zero, L 1 jr 9/9/2004 $ra # i in $t 0 = 0 # # # address of src[i] in $t 1 t 2 = src[i] address of dst[i] in $t 1 dst[i] = $t 2 i = i+1 if dst[i] != 0 repeat # return Comp 120 Fall 2004 20
Text Segment (main) #void main() { strcpy(S 2, S 1); }. text. globl main: addi $sp, -4 sw $ra, 0($sp) la $a 0, S 2 la $a 1, S 1 jal strcpy lw $ra, 0($sp) addi $sp, 4 jr $ra 9/9/2004 # # # # get space on the stack save main's return address of S 2 in the first argument address of S 1 in the second argument call strcpy restore main's return address restore the stack pointer exit main Comp 120 Fall 2004 21
How to write assembly programs • Develop the algorithm in a higher-level language (C/Java/Whatever) • Translate statement by statement to assembly • Use “Code Patterns” to guide the translation • Only a few idioms to master • Then it is just a mechanical process 9/9/2004 Comp 120 Fall 2004 22
Code Pattern for IF if(COND_EXPR) { STMTS 1 } else { STMTS 2 } CONDCODE(COND_EXPR, Lfalse 1, Ltrue 2) Ltrue 2: CODE(STMTS 1) j Lnext 3 Lfalse 1: CODE(STMTS 2) Lnext 3: 9/9/2004 Comp 120 Fall 2004 23
Code Pattern for Conditional Expr Comparisons: == != < > <= >= Conjunctions: && || Parenthesis: ( ) == get left in reg A A == B A B get right in reg B bne $A, $B, Lfalse get left in reg A < get right in reg B A<B A B slt $t 0, A, B beq t 0, $zero, Lfalse 9/9/2004 Comp 120 Fall 2004 24
CP for && and || && CONDCODE(L, Lfalse, Lnew 1) L && R Lnew 1: CONDCODE(R, Lfalse, Ltrue) || CONDCODE(L, Lnew 1, Ltrue) L || R Lnew 1: CONDCODE(R, Lfalse, Ltrue) 9/9/2004 Comp 120 Fall 2004 25
Example Conditional if(A != B && A > C) { ST } else { SE } A=s 1, B=s 2, C=s 3 beq && $s 1, $s 2, Lfalse 23 Lnew 24: > != A B A slt $t 0, $s 3, $s 1 C beq $t 0, $zero, Lfalse 23 Ltrue 25: CODE(ST) j Lnext 27 Lfalse 23: CODE(SE) Lnext 27: 9/9/2004 Comp 120 Fall 2004 26
While while(COND_EXPR) { STMTS; } Lwhile 44: CONDCODE(COND_EXPR, Lfalse 45, Ltrue 46) Ltrue 46: CODE(STMTS) j Lwhile 44 Lfalse 45: 9/9/2004 Comp 120 Fall 2004 27
FOR for(INIT; COND_EXPR; UPDATE) { STMTS } CODE(INIT) Lfor 17: CONDCODE(COND_EXPR, Lnext 18, Ltrue 19) Ltrue 19: CODE(STMTS) CODE(UPDATE) j Lfor 17 Lnext 18: 9/9/2004 Comp 120 Fall 2004 28
Functions int foo(int a, int b, int c, int d) { STMTS; return EXPR; } we know a=$a 0, b=$a 1, c=$a 2, d=$a 3 assign other simple variables to registers as possible foo: save any of $ra or $s 0 -s 7 that are overwritten CODE(STMTS) CODE(EXPR) leave result in $v 0 restore $ra, and $s 0 -s 7 if we saved them earlier jr $ra 9/9/2004 Comp 120 Fall 2004 29
cfind int cfind(char str[], char c) { for(int i=0; str[i] != 0; i++) if(str[i] == c) return i; return -1; } a 0 == address of str a 1 == value of character c v 0 == i no need to save anything 9/9/2004 Comp 120 Fall 2004 30
Expand function template cfind: # no need to save anything CODE(‘for. . . ’) CODE(‘return -1’) jr $ra 9/9/2004 Comp 120 Fall 2004 31
Expand for cfind: CODE(‘i=0’) Lfor 1: CONDCODE(‘s[i] != 0, Lnext 2, Ltrue 3) Ltrue 3: CODE(‘if. . . ’) CODE(‘i++’) j Lfor 1 Lnext 2: CODE(‘return -1’) jr $ra 9/9/2004 Comp 120 Fall 2004 32
Expand for INIT cfind: move $v 0, $zero Lfor 1: CONDCODE(‘s[i] != 0’, Lnext 2, Ltrue 3) Ltrue 3: CODE(‘if. . . ’) CODE(‘i++’) j Lfor 1 Lnext 2: CODE(‘return -1’) jr $ra 9/9/2004 Comp 120 Fall 2004 33
Expand for COND_EXPR cfind: # no need to save anything move $v 0, $zero Lfor 1: add $t 0, $a 0, $v 0 lb $t 1, 0($t 0) beq $t 1, $zero, Lnext 2 Ltrue 3: CODE(‘if. . . ’) CODE(‘i++’) j Lfor 1 Lnext 2: CODE(‘return -1’) jr $ra 9/9/2004 # address of s[i] # t 1 = s[i] Comp 120 Fall 2004 34
Expand if cfind: move $v 0, Lfor 1: add $t 0, lb $t 1, beq $t 1, Ltrue 3: $zero $a 0, $v 0 0($t 0) $zero, Lnext 2 # address of s[i] # t 1 = s[i] CONDCODE(‘s[i] == c’, Lfalse 4, Ltrue 5) Ltrue 5: CODE(‘return i’); Lfalse 4: CODE(‘i++’) j Lfor 1 Lnext 2: CODE(‘return -1’) jr $ra 9/9/2004 Comp 120 Fall 2004 35
Expand if COND_EXPR cfind: move $v 0, Lfor 1: add $t 0, lb $t 1, beq $t 1, Ltrue 3: $zero $a 0, $v 0 0($t 0) $zero, Lnext 2 # address of s[i] # t 1 = s[i] bne $t 1, $a 1, Lfalse 4 Ltrue 5: CODE(‘return i’); Lfalse 4: CODE(‘i++’) j Lfor 1 Lnext 2: CODE(‘return -1’) jr $ra 9/9/2004 Comp 120 Fall 2004 36
Expand return cfind: move $v 0, Lfor 1: add $t 0, lb $t 1, beq $t 1, Ltrue 3: bne $t 1, Ltrue 5: jr $ra $zero $a 0, $v 0 0($t 0) $zero, Lnext 2 # address of s[i] # t 1 = s[i] $a 1, Lfalse 4 # return i already in v 0 Lfalse 4: CODE(‘i++’) j Lfor 1 Lnext 2: CODE(‘return -1’) jr $ra 9/9/2004 Comp 120 Fall 2004 37
Expand i++ cfind: move $v 0, Lfor 1: add $t 0, lb $t 1, beq $t 1, Ltrue 3: bne $t 1, Ltrue 5: jr $ra Lfalse 4: addi $zero $a 0, $v 0 0($t 0) $zero, Lnext 2 # address of s[i] # t 1 = s[i] $a 1, Lfalse 4 # return i already in v 0 $v 0, 1 # i = i+1; j Lfor 1 Lnext 2: CODE(‘return -1’) jr $ra 9/9/2004 Comp 120 Fall 2004 38
Expand return -1 cfind: move $v 0, $zero Lfor 1: add $t 0, $a 0, $v 0 # address of s[i] lb $t 1, 0($t 0) # t 1 = s[i] beq $t 1, $zero, Lnext 2 Ltrue 3: bne $t 1, $a 1, Lfalse 4 Ltrue 5: jr $ra # return i already in v 0 Lfalse 4: addi $v 0, 1 # i = i+1; j Lfor 1 Lnext 2: subi jr 9/9/2004 $v 0, $zero, 1 # return value = -1 $ra Comp 120 Fall 2004 39
Remove unused labels cfind: move $v 0, $zero # i = 0 Lfor 1: add $t 0, $a 0, $v 0 # address of s[i] lb $t 1, 0($t 0) # t 1 = s[i] beq $t 1, $zero, Lnext 2 # if s[i] == 0 bne $t 1, $a 1, Lfalse 4 # if s[i] == c jr $ra # return i already in v 0 Lfalse 4: addi $v 0, 1 # i = i+1; j Lfor 1 Lnext 2: subu $v 0, $zero, 1 # return -1 jr $ra 9/9/2004 Comp 120 Fall 2004 40
- Slides: 40