The MIPS architecture embodies the fundamental design principles
The MIPS architecture embodies the fundamental design principles of all contemporary RISC architectures. • All basic instructions are directly executed in hardware. • The rate at which instructions are issued is maximized( usually one instruction per clock cycle). • Instructions are easy to decode. • Only load and store instructions reference memory. • Plenty of general purpose registers are provided (32 for MIPS).
Benefits of Studying Assembly Language Programming ● Obtain Insights into writing more efficient code or sometimes we may increase efficiency by modifying the codes generated by compilers. Will become familiar with what compilers do. ● Acquire an understanding of how a RISC CPU is organized. Open new opportunities in the field of embedded processors. ●
http: //www. mips. com/ The most likely area where assembly language programming is done is with embedded Processors, and MIPS has the largest share of the 64 -bit embedded processor market.
Going to the web site http: //www. mips. com/ you will find that the MIPS processor is used in: • Cisco Routers • Laser Printers built by HP and Fuji Xerox • PDA’s • Set-Top Boxes • Sony AIBO™ Entertainment Robot • Minolta Digital Camera • Sony Play. Station
MIPS
MIPS Instructions and Macros See Appendix C in the textbook#2 for a detailed description of every instruction and macro. • • Arithmetic instructions Branch Instructions( including function call) Load Instructions Store Instructions Move Instructions Logical Instructions Set Instructions Other Instructions
Programs in MIPS Assembly Language Programs in MIPS assembly language that can be executed by Spim MIPS simulators ( PCSpim for Window, Xspim for Unix and linux see http: //pages. cs. wisc. edu/~larus/spim. html) Use four types of instructions: MIPS instructions, such as add $v 1, $a 1, $s 1 MIPS macros, such as Load address la $s 0, label Macro is an operation which is equivalent to one or more MIPS instructions. An assembly programmer may use the macros along with the MIPS instruction in assembly program. During the assembling ( conversion assembly program to machine language), each macro is replace by the equivalent MIPS instructions. It is very similar to online functions in C++. Note that functions or methods are assembled separately from the caller functions and no replacement is performed.
Programs in MIPS Assembly Language(cont. ) Assembler Directives, For defining and storing data that can be accessed by the program at run time System I/O services System calls for inputting and outputting data.
Examples of MIPS instructions Let us suppose that an assembly language programmer wants to add the contents of register $a 1 to the contents of register $s 1, and to place the result in register $v 1. The assembly language instruction to accomplish this is: add $v 1, $a 1, $s 1 which is equivalent to the pseudocode statement: $v 1 = $a 1 + $s 1 mult $t 0, $t 1 which performs the multiplication of $t 0 and $t 1 and stores the result which is 64 bits In the Registers LOW and HIGH, LOW for storing the least significant 32 bits and HIGH for storing the most significant 32 bits.
Examples of MIPS instructions (cont. ) • div $t 0, $t 1 which performs the division of content $t 0 over the content of $t 1, and stores the quotient in LOW and reminder in HIGH. mfhi $v 0 which moves the content of HIGH to the register $v 0. • mflo $v 1 which moves the content of LOW to the register $v 1.
Quick Reference – Appendix A Integer Instruction Set. OPTIONAL Name Add: Add Immediate Unsigned: Add Unsigned: And Immediate: Branch if Equal: Branch if Greater Than or Equal to Zero and Link: Branch if Greater Than Zero: Branch if Less Than or Equal to Zero: Branch if Less Than Zero and Link: Branch if Less Than Zero: Branch if Not Equal: Divide Unsigned: Jump and Link: Jump and Link Register: Jump Register: Syntax addiu addu andi beq bgezal bgtz blez bltzal bltz bne divu j jalr jr Rd, Rs, Rt Rt, Rs, Imm Rs, Rt, Label Rs, Label Rs, Rt Rs, Rt Label Rd, Rs Rs Space/Time 1/1 1/1 1/1 1/1 1/38 1/1 1/1
Integer Instruction Set. OPTIONAL Name Load Byte: Load Byte Unsigned: Load Halfword Unsigned: Load Upper Immediate: Load Word Left: Load Word Right: Move From Coprocessor 0 Move From High: Move From Low: Move To Coprocessor 0 Move to High: Move to Low: Multiply Unsigned: NOR: OR Immediate: Return From Exception: Store Byte: Store Halfword: Shift Left Logical Variable: lb lbu Syntax Rt, offset(Rs) lhu Rt, offset(Rs) lui Rt, Imm lw Rt, offset(Rs) lwl Rt, offset(Rs) lwr Rt, offset(Rs) mfc 0 Rd, Cs mfhi Rd mflo Rd mtc 0 Rt, Cd mthi Rs mtlo Rs mult Rs, Rt multu Rs, Rt nor Rd, Rs, Rt ori Rt, Rs, Imm rfe sb Rt, offset(Rs) sh Rt, offset(Rs) sll Rd, Rt, sa sllv Rd, Rt, Rs Space/Time 1/1 1/1 1/1 1/1 1/32 1/1 1/1
Integer Instruction Set. OPTIONAL Name Set on Less Than: Set on Less Than Immediate Unsigned: Set on Less Than Unsigned: Shift Right Arithmetic Variable: Shift Right Logical Variable: Subtract Unsigned: Store Word Left: Store Right: System Call: Exclusive OR Immediate: srav Syntax sltiu sltu sra Rd, Rt, Rs srlv subu sw swl swr syscall xori Rd, Rt, Rs, Imm Rd, Rt, Rs Rd, Rt, sa 1/1 Rd, Rt, sa Rd, Rt, Rs Rd, Rs, Rt Rt, offset(Rs) Rd, Rs, Rt Rt, Rs, Imm Space/Time 1/1 1/1 1/1 1/1
Examples of Macro Instructions • Load Address la $s 0, table //loads the Register $s 0 with the value that //represents the address of label (location) table. • Load Immediate li $v 0, 10 //Loads the Register $v 0 with the immediate //value 10 • Move mov $t 8, $sp //Moves the content of $sp to $t 8(note that the content $sp of does not change).
Example of MIPS Instructions for Conditions There are instructions to implement control structures such as: “if. . . then. . . else. . . ” Let us suppose that if the contents of register $s 6 is less than zero, in other words negative, we want to branch to a location in the program labeled “Quit. ” Otherwise (else) we want to decrement the contents of register $s 6. if ($s 6 >= 0) then ($s 6 = $s 6 – 1) else goto Quit The assembly language instructions to accomplish this are: bltz $s 6, Quit addi $s 6, -1 Quit: ________
Integer Multiplication in MIPS The multiply instruction “mult” multiplies two 32 -bit binary values and produces a 64 -bit product which is stored in two special registers named High and Low. In this case, the destination for the result is implicitly understood. Register High will be loaded with the upper 32 -bits of the product and register Low will be loaded with the lower 32 -bits of the product. To move a copy of the value in the High register to the register file we use the instruction mfhi and to move a copy of the value in the Low register to the register file we use the instruction mflo. The following code segment shows how the 64 -bit product of $a 1 times $s 1 can be moved into $v 0 and $v 1: mult $a 1, $s 1 mfhi $v 0
Integer Division in MIPS The following divide instruction divides the 32 -bit binary value in register $a 1 by the 32 -bit value in register $s 1. The quotient is stored in the Low register and the remainder is stored in the High register. The following code segment shows how the quotient is moved into $v 0 and the remainder is moved into $v 1: div $a 1, $s 1 mflo $v 0 mfhi $v 1 In the MIPS architecture, division by zero is undefined. If it is possible that the divisor could be zero, then it is the programmers responsibility to test for this condition and to provide code to handle this special situation.
Example of MIPS Assembly Language Program to find the sum of the integers from 1 to 99 Label Op-Code Dest. S 1, S 2 . text main: move $a 0, $0 li $t 0, 99 loop: add $a 0, $t 0 addi $t 0, -1 bnez $t 0, loop li $v 0, 1 syscall li $v 0, 10 syscall Comments # $a 0 = 0 # $t 0 = 99 # $a 0 = $a 0 + $t 0 # $t 0 = $t 0 - 1 # if ($t 0 != zero) branch loop # Print the value in $a 0 # Terminate Program Run
Memory Addressing Modes The MIPS architecture is a Load/Store architecture, which means the only instructions that access main memory are the load and store instructions. Only one addressing mode is implemented in the hardware. The addressing mode is referred to as base address plus displacement.
Load Instruction A load instruction accesses a value from memory and places a copy of the value found in memory in the register file. For example, the instruction: lw $s 1, 8($a 0) $s 1 Mem[$a 0+8] computes the effective address of the memory location to be accessed by adding together the contents of register $a 0 (the base address) and the constant value eight (the displacement). A copy of the value accessed from memory at the effective address is loaded into register $s 1. The equivalent pseudocode statement would be: $s 1 Mem[$a 0 + 8] Or $s 1 = Mem[$a 0 + 8] (book’s notation)
Store Instruction The following is an example of a “Store Word” instruction: sw $s 1, 12($a 0) When the hardware executes this instruction it will compute the effective address of the destination memory location by adding together the contents of register $a 0 and the constant value 12. A copy of the contents of register $s 1 is stored in memory at the effective address. The equivalent pseudocode statement would be: Mem[$a 0 + 12] $s 1 or Mem[$a 0 + 12] = $s 1 From the point of view of an assembly language programmer, memory can be thought of as a very long linear array of locations where binary codes are stored. An effective address is a pointer to some location in this array.
Fetch Execute Cycle A Register Transfer Description of the Control Logic. OPTIONAL IR = Mem[PC] PC = PC + 4 lw or sw Decode Instruction beqz Read from Reg. File Address = Rs + Offset R-Type sw lw If (Rs == 0 ) then PC = PC + Offset Memory[Address] = Rt Reg. File[Rt] = Memory[Address] Reg. File[Rd] = Rs operation Rt
Pseudocode Using Pseudocode to Document a MIPS Assembly Language Program When documenting an algorithm in a language such as C, programmers use descriptive variable names such as: speed, volume, size, count, amount, etc. After the program is compiled, these variable names correspond to memory locations. To efficiently execute code, a compiler will attempt to keep the variables that are referenced most often in processor registers because access to a variable in a processor register is much faster than access to memory. MIPS has 32 processor registers. The names used to reference these registers are defined in Table 1. 1 in the textbook(#2) or in the brief summary at the course website.
Pseudocode (cont. ) As an assembly language programmer you must take maximum advantage of the processor registers. When using pseudocode to document an assembly language program, you will be expected to use the names of the registers you intend to use in the assembly language code. It is advisable to create a cross reference table between the processor register name and what it is being used for in the program.
Pseudocode (cont. ) We use register names in pseudocode because the purpose of the pseudocode is to document an assembly language program. Unless you identify the registers being used, the pseudocode is quite limited in terms of having any correspondence to the assembly language code. You will also find that as soon as you are able to develop the pseudocode in this format it is a very simple process to translate pseudocode into assembly language code. For example, For (t 0=1; t 0 < s 0; t 0++) do {this block of code};
Pseudocode (cont. ) Pseudocode for assembly language programs will have the appearance of C in terms of control structures and arithmetic expressions, but descripti variable names will usually only appear in the LOAD ADDRESS (la) instruction where there is a reference to a symbolic memory address. In assembly language you define and allocate space for variables in the data segment of memory using assembler directives such as. word and. space. You will find that all of the MIPS instructions require the use of processor registers. When writing pseudocode you should specify the processor registers you are planning to use to accomplish the task.
MIPS Assembly Language Syntax , [label: ] Op-Code [operand], [operand] [#comment] Label Op-Code Dest. S 1, S 2 po: add $a 0, $t 1 Comment # a 0 = t 0 + t 1
Translation an Arithmetic Expression $s 0 = srt ( $a 0 * $a 0 + $a 1 * $a 1) mult $a 0, $a 0 # Square $a 0 mflo $t 0 # t 0 = Lower 32 -bits of product mult $a 1, $a 1 # Square $a 1 mflo $t 1 # t 1 = Lower 32 -bits of product add $a 0, $t 1 # a 0 = t 0 + t 1 jal srt # Call the square root function // to call any function use jal function’s name. This statement will place //the starting address of the function (function’s name) in the register PC and //stores the return address which is the address of the next instruction (i. e. //the address of the instruction the comes immediately after jal srt) // # By convention, the result of sqr is returned in $v 0. // $v 0 and $v 1 is used to //store the return values from function calls, and registers $a 0 -$a 3 are used fot passing arguments (parameters’ values) to functions.
Translation of an “if … then … else …” Control Structure if ($t 8 < 0) then {$s 0 = 0 - $t 8; $t 1 = $t 1 +1} else {$s 0 = $t 8; $t 2 = $t 2 + 1} bgez $t 8, else # if ($t 8 is greater than or # equal to zero) branch to else sub $s 0, $zero, $t 8 # $s 0 gets the negative of $t 8 addi $t 1, 1 # increment $t 1 by 1 b next # branch around the else code else: move $s 0, $t 8 addi $t 2, 1 next: # $s 0 gets a copy of $t 8 # increment $t 2 by 1
Translation of a “while” Control Structure while ($a 1 < $a 2) do { $a 1 = $a 1 + 1 $a 2 = $a 2 - 1} while: bgeu addi b done: $a 1, $a 2, done # If( $a 1 >= $a 2) Branch to done $a 1, 1 # $a 1 = $a 1 + 1 $a 2, -1 # $a 2 = $a 2 - 1 while # Branch to while
Translation of a “for” Loop Control Structure $a 0 = 0; for ( $t 0 =10; $t 0 > 0; $t 0 = $t 0 -1) do {$a 0 = $a 0 + $t 0} li li $a 0, 0 $t 0, 10 # $a 0 = 0 # Initialize loop counter to 10 addi bgtz $a 0, $t 0, -1 # Decrement loop counter $t 0, loop # If ($t 0 > 0) Branch to loop:
Assembler Directives Assembler directives are not MIPS instructions and neither MIPS macro (i. e. are not executed by the MIPS processor at “run time”). They are simply direct the assembler to establish some data structures before run time. All directives can be identified by the fact that they begin with a period symbol. For example, to allocate space in memory for a one-dimensional array of 1024 integers, the following construct is used in the C language: int ARRAY[1024] ; In MIPS assembly language, the corresponding construct is: . data ARRAY: . space 4096 //(WHY 4096? )
Examples Assembler Directives To initialize a memory array before program execution begins with a set of 16 values corresponding to the powers of 2 ( 2 N with N going from 0 to 15) , the following construct is used in the C language: Int Pof 2[16] ={ 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 } In MIPS assembly language the corresponding construct is: . data Pof 2: . word 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 Also, it is possible to use. word to store addresses represented by symbols, their values determined by the assembler. In general . word w 1, w 2, w 3, …, wn //means stores the n 32 -bit w 1, w 2, w 3, …, wn quantities in successive memory words.
Examples Assembler Directives (cont. ) • Here is an example of how MIPS code can be written to access element two in the array and place a copy of the value in reg. $s 0. The load address la macro instruction is used to initialize a pointer in “$a 0” with the base address of the array labeled “Pof 2. ” la $a 0, Pof 2 # a 0 = &Pof 2(& stands for address) // the previous statement loads the Register $a 0 with the address of location Pof 2. lw $s 0, 8($a 0) # s 0 = MEM[a 0 + 8] This instruction loads the third value of the array to the register $s 0. If we replace 8 by 4, the second value of the array will be loaded. lw $s 0, (Sa 0) will load the first value of the array.
Examples Assembler Directives (cont. ) . data<addr>// subsequent items are stored by the simulator in the data segment (in memory). If the optional argument addr is present, subsequent items are stored starting at address addr. For example. data 0 x 00008000, note that 0 x indicates the address in Hex and each digit of the eight digits is a Hex digit. Note that numbers are base 10 by default. . text <addr>// Subsequent items are put by the simulator in the user text segment (in memory). In SPIM, these items may only be instructions or words as defined in the previous slide. If the optional argument addr is present, subsequent items are stored starting at address addr
Examples Assembler Directives (cont. ) • . align n// means align the next datum on 2 n byte boundary. For example. align 2 aligns the next value on a word boundary. . align 0 turns off automatic aligement of. half, . word, . float and. double directives until the next . data or. kdata directives (see Appendix A of Text#2). • . ascii string// means store the string enclosed in double quotes in memory, but do not null-terminate it. • . asciiz string// means store the string enclosed in double quotes in memory and null-terminate it. • For strings, the special characters follow the C convention: newline(n), tab (t), quote(”)
Examples Assembler Directives (cont. ) • . byte b 1, …bn . float f 1, …. . fn . double d 1, …dn . half h 1, …hn Each of the above directive means store the n (8 -bit, single-precision floating number, double-precision floating number, or 16 -bit quantities) in successive (bytes, memory locations (required size ? ), memory locations (required size? ), or half words).
Examples Assembler Directives (cont. ) • . global Symb// declares that the label Symb is global so it can be referenced from other files. . ktext <addr>//Subsequent items are put by the simulator in the kernal text segment (in memory). In SPIM, these items may only be instructions or words. If the optional argument addr is present, subsequent items are stored starting at address addr. . extern Symbol size //declares that the data stored in Symb is of size bytes large and is a global label. This directive enables the assembler to store data in a portion of the data segment that efficiently accessed via Register $gp.
Input/Output System Calls • The MIPS Simulator SPIM described in Chapter 4 provides a set of system calls to perform input, output, and exiting from program (stop). The Registers $a 0, $a 1, $f 1, and $v 0 are used in these operations. • To perform one of these operations we must load Register $v 0 with the appropriate code(1 -10) and placing the integer (or string) or float to be printed in Register $a 0 or $f 12, respectively. Similar steps must performed for reading and exit. The following table shows some of the codes and their meaning. A complete table of all system calls is in Appendix A(Text#2).
Input/Output System Calls (cont. ) Service Code Arguments and Results Print_integer 1 $a 0 = Integer to be printed Print_string 4 $a 0 = Address of string in memory Read_integer 5 $v 0= Integer that entered by user Read_string 8 $a 0 = Address of input buffer in memory $a 1 = Length of buffer Exit 10
A Complete Example • Now for an example, let us suppose that we want to write an assembly language program to find the sum of the integers from 1 to N. In other words do the following: 1 + 2 + 3 + 4 + 5 + 6 + 7 +. . + N, where “N” is an input value. • On the next slide you will see a pseudocode description of the algorithm and following that the corresponding assembly language program, where processor register $t 0 is used to accumulate the sum, and processor register $v 0 is used as a loop counter. • Use a word processor (notepad) to create the following program file. Be sure to save as text only. • Next, load the program into SPIM. Run the program and experiment with the different features of the MIPS simulator. ( For example: Single Step) • Read the help file for a description of how to use the simulator.
A Complete Example ################################# # Program Name: Sum of Integers # Programmer: YOUR NAME # Date last modified: ################################# # Functional Description: # A program to find the Sum of the Integers from 1 to N, where N is a value # read in from the keyboard. ################################# # Pseudocode description of algorithm: # main: cout << “n Please input a value for N = ” # cin >> v 0 # If ( v 0 > 0 ) # {t 0 = 0; # While (v 0 > 0 ) do # {t 0 = t 0 + v 0; # v 0 = v 0 – 1} # cout << “ The sum of the integers from 1 to N is ”, t 0; # go to main # } # else # cout << “n **** Adios Amigo - Have a good day ****” ################################# # Cross References:
###################################. data Prompt: . asciiz “n Please Input a value for N = ” Result: . asciiz “ The sum of the integers from 1 to N is ” Bye: . asciiz “n **** Adios Amigo - Have a good day ****”. globl main. text main: li $v 0, 4 # system call code for Print String la $a 0, Prompt # load address of prompt into $a 0 syscall # print the prompt message li $v 0, 5 # system call code for Read Integer syscall # reads the value of N into $v 0 blez $v 0, End # branch to end if $v 0 < = 0 li $t 0, 0 # clear register $t 0 to zero Loop: add $t 0, $v 0 # sum of integers in register $t 0 addi $v 0, -1 # summing integers in reverse order bnez $v 0, Loop # branch to loop if $v 0 is != zero li $v 0, 4 # system call code for Print String la $a 0, Result # load address of message into $a 0 syscall # print the string li $v 0, 1 # system call code for Print Integer move $a 0, $t 0 # move value to be printed to $a 0 syscall # print sum of integers b main # branch to main
End: li la syscall li syscall $v 0, 4 $a 0, Bye $v 0, 10 # system call code for Print String # load address of msg. into $a 0 # print the string # terminate program run and # return control to system
Example of reading, storing, and printing an array of four integers. . data array: . space 16 new. Line: . asciiz "n“ #/ I will use it to start a new line Prompt: . asciiz “ n Enter an integer “ . text main: li $t 0, 4 # $t 0 for keep track the number of integers to be read. la $t 1, array #/ loading the starting address of the array loop. R: la $a 0, Prompt li $v 0, 4 syscall li $v 0, 5 # reading an integer syscall sw $v 0, 0($t 1) # storing the integer entered add $t 0, -1 # decrement the number of integers by one add $t 1, 4 # load the address of the next integer bgtz $t 0, loop. R # branch to read and store the next integer li $t 0, 4 la $t 1, array loop. P: lw $a 0, 0($t 1 )# load an integer from memory location to $a 0 li $v 0, 1 syscall # print the integer add $t 0, -1 add $t 1, 4 la $a 0, new. Line # start a new line li $v 0, 4
Translation of a “switch” Control Structure $s 0 = 32; top: cout << “Input a value from 1 to 3” cin >> $v 0 // cin and cout are two statements of C++ for //inputting and outputting, respectively. switch ($v 0) { case(1): {$s 0 = $s 0 << 1; break; } case(2): {$s 0 = $s 0 << 2; break; } case(3): {$s 0 = $s 0 << 3; break; } default: goto top; } cout << $s 0
Translation of a “switch” Control Structure (cont. ) • $s 0 << k means shift left the content of $s 0 k number of times. What is the effect of $s 0<<1 1 if the content of $s 0 is an integer? • $s 0 >> k means shift right the content (binary representation) of $s 0 k number of times. What is the effect of $s 0>>1 if the content of $s 0 is an integer?
Translation of a “switch” Control Structure (cont. )( to be covered later) jumptable: . word prompt: . data. align 2 top, case 1, case 2, case 3. asciiz “nn Input a value from 1 to 3: ”. text top: case 1: case 2: case 3: output: li la syscall li syscall blez li bgt la sll add lw jr sll b sll $v 0, 4 $a 0, prompt # Code to print a string $v 0, 5 # Code to read an integer li $v 0, 1 $v 0, top # Default for less than one $t 3, 3 $v 0, $t 3, top # Default for greater than 3 $a 1, jumptable # Load address of jumptable $t 0, $v 0, 2 # Compute word offset (multiply by 4) $t 1, $a 1, $t 0 # Form a pointer into jumptable $t 2, 0($t 1) # Load an address from jumptable $t 2 # Jump to specific case “switch” $s 0, 1# Shift left logical one bit output $s 0, 2# Shift left logical two bits output $s 0, 3# Shift left logical three bits # Code to print an integer is 1
Solutions to Chap 2 Exercises OPTIONAL label op-code Rd, Rs, Rt a: b: c: add sub addi div mflo addi $t 3, $t 4, $t 5 $t 3, $t 6 $s 3, $s 1, -4321 $t 2, $s 3 $sp, -16 d: move $a 0, $t 3 li $v 0, 1 syscall e: li $v 0, 5 syscall move $t 0, $v 0
Solutions to Chap 2 Exercises label op-code Rd, Rs, Rt. OPTIONAL f: la $a 0, array g: lw li sw $t 8, 0($a 0) h: $t 0, 32768 $t 0, 16($a 0) i: . data Msg: . asciiz “Hello World”. text li $v 0, 4 la $a 0, Msg syscall
Solutions to Chap 2 Exercises If (t 0 < 0) then t 7 = 0 – t 0 else t 7 = t 0; OPTIONAL (j) bgez $t 0, else sub $t 7, $0, $t 0 b next else: move $t 7, $t 0 (k) while ( t 0 != 0) { s 1 = s 1 + t 0; t 2 = t 2 + 4; t 0 = Mem(t 2) }; while: done: beqz addi lw b $t 0, done $s 1, $t 0 $t 2, 4 $t 0, 0($t 2) while
(l) Solutions to Chap 2 Exercises for ( t 1 = 99; t 1 > 0; t 1 = t 1 -1) v 0 = v 0 + t 1; li $t 1, 99 OPTIONAL addi bgtz $v 0, $t 1, -1 $t 1, loop: (m) t 0 = 2147483647 - 2147483648; li $t 0, -1 (n) s 0 = -1 * s 0; sub $s 0, $s 0
Exercises – Chapter 2 (o) s 1 = s 1 * a 0; OPTIONAL mult $s 1, $a 0 mflo $s 1 (p) s 2 = srt(s 02 + 56) / a 3; mult $s 0, $s 0 mflo $a 0 addi $a 0, 56 jal srt div $v 0, $a 3 mflo $s 2
Exercises – Chapter 2 (q) s 3 = s 1 - s 2 / s 3; OPTIONAL div $s 2, $s 3 mflo $t 0 sub $s 3, $s 1, $t 0 (r) s 4 = s 4 * 8; sll (s) $s 4, 3 s 5 = 7 * s 5; sll add $t 1, $s 5, 1 $t 2, $s 5, 2 $t 1, $t 2 $s 5, $t 1
Exercises Continued 2. 3 Show the following expression can be evaluated in MIPS assembly language, without modifying the contents of the “s” registers: $t 0 = ( $s 1 - $s 0 / $s 2) * $s 4 ; OPTIONAL div $s 0, $s 2 mflo $t 0 sub $t 0, $s 1, $t 0 mult $t 0, $s 4 mflo $t 0
2. 4 Show the following expression can be efficiently evaluated in MIPS assembly language. OPTIONAL $t 0 = $s 0/ 8 – 2*$s 1 + $s 2 sra sll sub add $t 0, $s 0, 3 $t 1, $s 1, 1 $t 0, $t 1 $t 0, $s 2
Clock Cycles Exercises 2. 3 & 2. 4 2. 3 2. 4 OPTIONAL div mflo sub mult mflo sra sll sub add $s 0, $s 2 $t 0, $s 1, $t 0, $s 4 $t 0, $s 0, 3 $t 1, $s 1, 1 $t 0, $t 1 $t 0, $s 2 Clock Cycles 38 1 1 32 1 Total= 73 1 1 Total= 4
Solution to “for loop” Exercise ############### Clock Cycles li $s 0, 1 OPTIONAL 1 la $s 1, array # 2 li $t 7, 40 # 1 loop: addu $t 1, $s 1, $t 7 # 1 lw $t 2, 0($t 1) # 1 mult $s 0, $t 2 # 32 mflo $s 0 # 1 addi $t 7, -4 # 1 bgtz $t 7, loop # 1 #
Solution to “while” Exercise ############## Clock Cycles. OPTIONAL li $t 1, 40 # 1 li $s 1, -2 # 1 la $s 2, array # 2 while: addu $t 7, $t 1, $s 2 # 1 lw $t 6, 0($t 7) # 1 mult $t 6, $s 1 # 32 mflo $s 1 # 1 addi $t 1, -4 # 1 bgtz $t 1, while # 1
Number Systems • • • Introduction Polynomial Expansion Binary Numbers Hexadecimal Numbers Two’s Complement Number System • Arithmetic & Overflow Detection • American Standard Code for Information Interchange (ASCII)
Polynomial Expansion of a Decimal Number (Base 10) 2 1 0 496 = 4 x 10 + 9 x 10 + 6 x 10 10 Polynomial Expansion of a Binary Number (Base 2) 5 4 3 2 1 0 00101101 = 1 x 2 + 0 x 2 + 1 x 2 + 0 x 2 + 1 x 2 2
Conversion of Decimal Numbers to Binary Divide by 2 and extract the remainder 45 Remainder 1 0 1 22 1 11 0 5 1 2 1 1 0 0 1
To represent binary values in the positive and negative domains we use the Two’s Complement Number System Here is the polynomial expansion of a two’s complement 8 -bit binary number N: N = - 0 3 2 1 d 7 x 2 + d 6 x 2 + d 5 x 2 + d 4 x 2 + d 3 x 2 + d 2 x 2 + d 1 x 2 +d 0 x 2 7 6 5 4 Notice the Minus sign !!!! *** You need to memorize powers of 2 ***
The Two’s Complement Operation When we take the two’s complement of a binary number, the result will be the negative of the value we started with. For example, the 8 -bit binary value 00011010 is 26 in decimal. To find the value negative 26 (-26) in binary we perform the two’s complement operation on 00011010. Scan the binary number from right to left leaving all least significant zeros (0) and the first one (1) unchanged, and then complement the remaining digits to the left: 11100110 The result is the value negative 26 (-26) in binary.
Exercises • Show the binary representation of the following decimal integers assuming 16 bits are available: 24 21 + 212 - 24 - 210 - 211
Binary Arithmetic & Overflow Detection in the Two’s Complement Number System Here is an addition example where we assume we are limited to 8 binary digits. 0100 + 00111100 = = 68 60 10000000 = -128 Overflow Occurred
Overflow 0000 1111 0001 0 1110 -1 0010 1 -2 2 0011 1101 3 -3 1100 -4 4 0100 -5 1011 5 -6 0101 6 1010 -7 7 -8 0110 0111 1000
Binary Arithmetic in the Two’s Complement Number System Here is a subtraction example where we assume we are limited to 8 binary digits. To subtract in binary we always add the two’s complement of the subtrahend. 0100 = 0100 -00111100 = +11000100 00001000 = 68 -60 8
The Rule for Detection of Overflow ######################### Adding numbers of opposite signs, overflow is impossible. When adding numbers of the same sign, if the result is not the same as the operands then overflow occurred. ######################### Here is an example: You are given the following two numbers in two’s complement representation. Perform the binary subtraction and indicate if there is signed overflow. ______Explain Why: 11101000 -00010011 11101000 = -24 +11101101 = -19 11010101 Correct Result = -43
Sign Extension The value – 43 as an 8 -bit binary number is: 11010101 The value – 43 as an 32 -bit binary number is: 1111111111111010101 In Hexadecimal – 43 appears as: 0 x. FFFFFFD 5 ######################## The value 68 as an 8 -bit binary number is: 0100 The value 68 as an 32 -bit binary number is: 0000000000000100 In Hexadecimal 68 appears as: 0 x 00000044
Print Integer • Print operation of the simulator always considers the integers as 2’s complement Integers for printing purpose.
The Hexadecimal Number System Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Hex 0 1 2 3 4 5 6 7 8 9 A B C D E F Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Here is an example of how we compactly represent binary numbers in hexadecimal: | | | 0011110010001111110 0 x 3 C 8 F 7 E
OPTIONAL 3. 1 Convert the decimal number 35 to an 8 -bit binary number. 00100011 3. 2 Convert the decimal number 32 to an 8 -bit binary number. 00100000 3. 3 Using the double and add method convert 00010101 to a decimal number. 21 3. 4 Using the double and add method convert 00011001 to a decimal number. 25 3. 5 Explain why the Least Significant digit of a binary number indicates LSD is a 1 if the number is odd or even. 3. 6 Convert the binary number 00010101 to a hexadecimal number. 0 x 15 3. 7 Convert the binary number 00011001 to a hexadecimal number. 0 x 19 3. 8 Convert the hexadecimal number 0 x 15 to a decimal number. 21 3. 9 Convert the hexadecimal number 0 x 19 to a decimal number. 25 3. 10 Convert the decimal number -35 to an 8 -bit two’s complement binary number. 1101
Exercises 3. 11 Convert the decimal number -32 to an 8 -bit two’s complement binary number. OPTIONAL 3. 12 Assuming the use of the two’s complement number system find the equivalent decimal values for the following 8 -bit binary numbers: (a) 10000001 (b) 1111 (c) 01010000 (d) 11100000 (e) 10000011 3. 13 Convert the base 8 number 204 to decimal 3. 14 Convert the base 7 number 204 to decimal 3. 15 Convert the base 6 number 204 to decimal 3. 16 Convert the base 5 number 204 to decimal 3. 17 Convert the base 10 number 81 to a base 9 number. 11100000 -127 -1 80 -32 -125 132 102 76 54 100
Exercises 3. 20 You are given the following two numbers in two’s complement representation. Perform the binary subtraction and indicate if there is signed overflow. ______ Explain Why: 11101000 -00010011 11101000 + 11101101 11010101 No Overflow 3. 21 Sign extend the 8 bit hex number 0 x 88 to a 16 bit number. 0 x_____ FF 88 3. 22 The following subtract instruction is located at address 0 x 00012344. What are the two possible values for the contents of the PC after the 00012340 branch instruction executed? 0 x_______ 0 x ______ 0001234 C This branch instruction is described in Appendix C. loop: addi $t 4, -8 sub $t 2, $t 0 bne $t 4, $t 2, loop
Exercises 3. 23 You are given the following two 8 -bit binary numbers in the two’s complement number system. What values do they represent in decimal? -108 44 X = 10010100 = _____ Y = 00101100 = _____ 2 10 Perform the following arithmetic operations on X and Y. Show your answers as 8 -bit binary numbers in the two’s complement number system. To subtract Y from X, find the two’s complement of Y and add it to X. Indicate if overflow occurs in performing any of these operations. X+Y X-Y Y-X 10010100 00101100 +11010100 +011011000000 01101000 10011000
Exercise 3. 24 The following code segment is stored in memory starting at memory location 0 x 00012344. What are the two possible values for the contents of the PC after the branch instruction has executed? 0 x___ ______ 00012344 0 x 00012354 Add in line pseudocode to describe each instruction. loop: lw addi andi beqz $t 0, 0($a 0) $a 0, 4 $t 1, $t 0, 1 $t 1, loop # # t 0 = Mem[a 0] a 0 = a 0 +4 t 1 = t 1 & 1 “Extract LSD” if t 0 is even go to loop
- Slides: 77