lecture7

lecture7 - CS64 Lecture 7 Logic/Shi2 + I/O Prof....

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS64 Lecture 7 Logic/Shi2 + I/O Prof. Heather Zheng HW 2 assigned today •  4 quesCons •  Q1 ­3: submit via HW dropbox •  Q4: submit the complete program via turnin, using hw2 •  Due: April 25, 4:30pm •  NOTE: Lab 1 ­3: connect ­4 will be assigned later this week. Review: Logical Operators •  Two basic logical operators: –  AND: outputs 1 only if both inputs are 1 –  OR: outputs 1 if at least one input is 1 •  In MIPS assembly, both of these accept exactly 2 inputs and produce 1 output –  Again, rigid syntax, simpler hardware Logical Operators • Truth Table: standard table lisCng all possible combinaCons of inputs and resultant output for each • Truth Table for AND and OR A B A AND B A OR B 0 0 0! 0! 0 1 1 1 0 1 0! 1! 0! 1! 1! 1! Logical Operators •  InstrucCon Names: –  and, or: Both of these expect the third argument to be a register –  andi, ori: Both of these expect the third argument to be an immediate •  MIPS Logical Operators are all bitwise, meaning that bit n of the output is produced by the respecCve bit n’s of the inputs, bit 1 by the bit 1’s, etc. QuesCon 1 What is the bit ­wise OR of the following pa]erns: 0110 1101 0100 1010 •  A. 0110 1111 •  B. 0111 0110 •  C. 0110 1001 •  D. 1110 0110 QuesCon 2 •  What is the bit ­wise AND of the following pa]erns: 0110 1101 0100 1010 •  A. 0110 0110 •  B. 0100 1000 •  C. 0110 1001 •  D. 1110 0110 QuesCon 3 Which of the following instrucCons loads register $5 with the bit pa]ern that represents posiCve decimal 48? •  A. ori $5,$0,0x48 •  B. ori $5,$5,0x48 •  C. ori $5,$0,48 •  D. ori $0,$5,0x48 QuesCon 4 •  Which of the following instrucCons clears all the bits in register $8 except the low order byte (the right most byte), which is unchanged? •  •  •  •  A. ori $8,$8,0x00FF B. ori $8,$0,0x00FF C. andi $8,$8,0xFFFF D. andi $8,$8,0x00FF Uses for Logical Operators •  Note that anding a bit with 0 produces a 0 at the output while anding a bit with 1 produces the original bit. •  This can be used to create a mask. –  Example: mask last 12 bits! 1011 0110 1010 0100 0011 1101 1001 1010 0000 0000 0000 0000 0000 1111 1111 1111 –  The result of anding these: 0000 0000 0000 0000 0000 1101 1001 1010 Uses for Logical Operators •  Similarly, note that oring a bit with 1 produces a 1 at the output while oring a bit with 0 produces the original bit. •  This can be used to force certain bits of a string to 1s. – For example, if $t0 contains 0x12345678, then a2er this instrucCon: ori $t0, $t0, 0xFFFF – … $t0 contains 0x1234FFFF (e.g. the high ­order 16 bits are untouched, while the low ­order 16 bits are forced to 1s). Shi2 InstrucCons •  MIPS shi2 instrucCons: 1. sll (shi2 le2 logical): shi2s le2 and fills empCed bits with 0s 2. srl (shi2 right logical): shi2s right and fills empCed bits with 0s 3. sra (shi2 right arithmeCc): shi2s right and fills empCed bits by sign extending Shi2 InstrucCons •  Example: shi2 right arithmeCc by 8 bits 0001 0010 0011 0100 0101 0110 0111 1000 0000 0000 0001 0010 0011 0100 0101 0110   Example: shift right arithmetic by 8 bits 1001 0010 0011 0100 0101 0110 0111 1000 1111 1111 1001 0010 0011 0100 0101 0110 QuesCon 5 Complete the program so that register $4 receives the pa]ern in register $5 shi2ed le2 logical by three posiCons. ori $5, $0, 0x92AF # put a bit pa(ern into register $5 sll ___, ___, ___ # shi2 le2 logical by three, put result in register $4 •  A. sll $5, $3, $4 •  B. sll 3, $5, $4 •  C. sll 5, 3, 4 •  D. sll $4, $5, 3 QuesCon 6 Let’s say $t0 holds the following 32 ­bit 0001 0010 0011 0100 0101 0110 0111 1000 aka: 0x12345678 A2er running the following 2 instrucCons, what happens to $t0? sll $t0,$t0,16 srl $t0,$t0,24 0001 0010 0011 0100 0101 0110 0111 1000 aka: 0x12345678 sll $t0,$t0,16 srl $t0,$t0,24 0001 0010 0011 0100 0101 0110 0111 1000 0101 0110 0111 1000 0000 0000 0000 0000 aka: 0x56780000 0000 0000 0000 0000 0000 0000 0101 0110 aka: 0x00000056 INPUT/OUTPUT System Calls Format li service call code print integer 1 print float 2 print double 3 print string 4 read integer 5 read float 6 read double 7 read string 8 sbrk 9 exit 10 arguments int in $a0 float in $a0 double in $a0 addr of string in $a0 $v0, 5 syscall results int in $v0 float in $v0 double in $v0 $a0=buffer, $a1=length $a0 = amount addr in $v0 # add2.asm ­ ­ A program that computes and prints the sum of two numbers specified at runCme by the user. # Registers used: # $t0  ­ used to hold the first number. # $t1  ­ used to hold the second num # $t2  ­ used to hold the sum of the $t1 and $t2. # $v0  ­ syscall parameter and return value. # $a0  ­ syscall parameter. main: ## Get first number from user, put into $t0. li $v0, 5 # load syscall read_int into $v0. syscall # make the syscall. move $t0, $v0 # move the number read into $t0. ## Get second number from user, put into $t1. li $v0, 5 # load syscall read_int into $v0. syscall # make the syscall. add $t1, $v0, $0 # move the number read into $t1. add $t2, $t0, $t1 # compute the sum. System Calls Format Add $a0, $s0, $0 li $v0, 1 syscall service call code print integer 1 print float 2 print double 3 print string 4 read integer 5 read float 6 read double 7 read string 8 sbrk 9 exit 10 arguments int in $a0 float in $a0 double in $a0 addr of string in $a0 results int in $v0 float in $v0 double in $v0 $a0=buffer, $a1=length $a0 = amount addr in $v0 ## Print out $t2. add $a0, $t2, $0 # move the number to print into $a0. li $v0, 1 # load syscall print_int into $v0. syscall # make the syscall. li $v0, 10 # syscall code 10 is for exit. syscall # make the syscall. # end of add2.asm. System Calls Format service call code print integer 1 print float 2 print double 3 print string 4 read integer 5 read float 6 read double 7 read string 8 sbrk 9 exit 10 arguments int in $a0 float in $a0 double in $a0 addr of string in $a0 results int in $v0 float in $v0 double in $v0 $a0=buffer, $a1=length $a0 = amount addr in $v0 Assembly Program of the Day .data prompt: .asciiz "Please input your name (20 char max): " statestr: .space 21 # an assembly direc;ve which reserves #memory (indicated in bytes) .text main: li $v0, 4 # code for print_string goes in $v0 la $a0, prompt # $ao= address of string to be printed syscall # print the prompt li $v0, 8 # code for read_string goes in $v0 la $a0, statestr # address of the input buffer in memory goes #in register $a0 li $a1, 20 # size of buffer goes in register $a1 syscall # read the input PROCEDURE C funcCons main() { int a,b,c; ... c = sum(a,b); $s0,$s1,$s2 */ ... } What information must
 compiler/programmer 
 keep track of?" /* a,b,c: /* really dumb sum function */ int sum(int x, int y) { What instructions can ! return x+y; accomplish this?" } Procedure calls Main Program … … … … … … … Function loc: … … … … … … … FuncCon Call Bookkeeping • Registers play a major role in keeping track of informaCon for funcCon calls • Register convenCons: – Return address $ra – Arguments $a0, $a1, $a2, $a3 – Return value $v0, $v1 – Local variables $s0, $s1, … , $s7 •  The stack is also used; more later InstrucCon Support for FuncCons ... sum(a,b);... /* a,b:$s0,$s1 */ } C! int sum(int x, int y) { return x+y; } M
 address In MIPS, all instructions are 4 bytes, and stored in memory just I
 1000 1004 like data. So here we show the P
 addresses of where the programs 1008 S! 1012 are stored.! 1016 Procedure calls Main Program … … … Sum(a,b) … … … … Function loc: … … … … … … … InstrucCon Support for FuncCons ... sum(a,b);... /* a,b:$s0,$s1 */ } C! int sum(int x, int y) { return x+y; } address 1000 add $a0,$s0,$zero # x = a 1004 add $a1,$s1,$zero # y = b M
 1008 addi $ra,$zero,1016 # $ra=1016 ??? I
 1012 j sum #jump to sum P
 1016 ... S! 2000 sum: add $v0,$a0,$a1 2004 jr $ra # new instruction InstrucCon Support for FuncCons ... sum(a,b);... /* a,b:$s0,$s1 */ } C! int sum(int x, int y) { return x+y; }   Question:   Answer: Why use jr here? Why not simply use j? M
 sum might be called by many functions, so I
 we can’t return to a fixed place. The calling proc to P
 sum must be able to say “return here” somehow. S! 2000 sum: add $v0,$a0,$a1 2004 jr $ra # new instruction InstrucCon Support for FuncCons •  Single instrucCon to jump and save return address: jump and link (jal) •  Before: 1008 addi $ra,$zero,1016 #$ra=1016? 1012 j sum #go to sum •  A2er: 1008 jal sum # $ra=1012?,go to sum •  Why have a jal? Make the common case fast: funcCon calls are very common. Also, you don’t have to know where the code is loaded into memory with jal. InstrucCon Support for FuncCons •  Syntax for jal (jump and link) is same as for j (jump): jal label •  jal should really be called laj for “link and jump”: – Step 1 (link): Save address of next instrucCon into $ra (Why next instrucCon? Why not current one?) – Step 2 (jump): Jump to the given label InstrucCon Support for FuncCons • Syntax for jr (jump register): jr register • Instead of providing a label to jump to, the jr instrucCon provides a register which contains an address to jump to • Only useful if we know exact address to jump • Very useful for funcCon calls: – jal stores return address in register ($ra) – jr $ra jumps back to that address InstrucCon Support for FuncCons ... sum(a,b);... /* a,b:$s0,$s1 */ } C! int sum(int x, int y) { return x+y; } address 1000 add $a0,$s0,$zero # x = a # y = b M
 1004 add $a1,$s1,$zero #jump to sum I
 1012 jal sum 1016 ... P
 2000 sum: add $v0,$a0,$a1 S! 2004 jr $ra # new instruction Steps for Making a Procedure Call 1) Save necessary values onto stack 2) Assign argument(s), if any 3) jal call 4) Restore values from stack Procedure calls caller $ra … … … jal loc … … … … callee loc: … … … … … … jr $ra … Example main() { int i,j,k,m; /* i-m:$s0-$s3 */ ... i = mult(j,k); ... m = mult(i,i); ... } int mult (int mcand, int mlier){ int product; product = 0; while (mlier > 0) { product += mcand; mlier -= 1; } return product;} Example __start: add $a0,$s1,$0 add $a1,$s2,$0 jal mult add $s0,$v0,$0 ... add add jal add ... done $a0,$s0,$0 $a1,$s0,$0 mult $s3,$v0,$0 main() { int i,j,k,m; /* i-m:$s0-$s3 */ ... i = mult(j,k); ... m = mult(i,i); ... } # arg0 = j # arg1 = k # call mult # i = mult() # # # # arg0 = i arg1 = i call mult m = mult() Example int mult (int mcand, int mlier){ int product = 0; while (mlier > 0) { product += mcand; mlier -= 1; } return product;} mult:add Loop:slt $t0,$0,$0 $t1,$0,$a1 beq $t1,$0,Fin add $t0,$t0,$a0 addi $a1,$a1,-1 j Loop Fin:add $v0,$t0,$0 jr $ra # # # # # prod=0 mlr > 0? no=>Fin prod+=mc mlr-=1 # goto Loop # $v0=prod # return Example •  Notes: – main funcCon ends with done, not jr $ra, so there’s no need to save $ra onto stack – All variables used in main funcCon are saved registers, so there’s no need to save these onto stack Rules for Procedures •  Called with a jal instrucCon, returns with a jr $ra •  Accepts up to 4 arguments in $a0, $a1, $a2 and $a3 •  Return value is always in $v0 (and if necessary in $v1) •  Must follow register convenCons (even in funcCons that only you will call)! So what are they? ...
View Full Document

This note was uploaded on 12/27/2011 for the course CMPSC 64 taught by Professor Zheng during the Fall '09 term at UCSB.

Ask a homework question - tutors are online