MIPS Assembly Language Programming

Please download to get full document.

View again

All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Information Report
Category:

General

Published:

Views: 2 | Pages: 0

Extension: PDF | Download: 0

Share
Related documents
Description
MIPS Assembly Language Programming. Lesson #1. Bob Britton, Instructor. Required Background Preparation. Two semesters of programming experience. Benefits of Studying Assembly Language Programming. Obtain Insights into writing more efficient code Will become familiar with what compilers do
Transcript
MIPS Assembly Language Programming Lesson #1 Bob Britton, Instructor Required Background Preparation Two semesters of programming experience Benefits of Studying Assembly Language Programming Obtain Insights into writing more efficient code Will become familiar with what compilers do Acquire an understanding of how computers are built Open new opportunities in the field of embedded processors Course Description Basic computer organization concepts such as registers, the ALU, data path, and random access memory (RAM). The use of pseudocode to develop and document algorithms at the register level. Number systems and the rules for arithmetic. Passing parameter to functions using the stack. Assemblers and linking editors. Modular program design and development. Introduction
  • Basic Computer Organization
  • Machine Language
  • Assembly Language
  • MIPS MIPS PowerPC MIPS Computer Organization
  • Datapath Diagram
  • Control Logic
  • DataPath Diagram Number Value Name Register File Return values from functions Pass parameters to functions Caller Saved Registers – Use these registers in functions Callee-Saved Registers – Use these registers for values that must be maintained across function calls. An Example MIPS Assembly Language Program LabelOp-CodeDest.S1,S2Comments move$a0, $0 # $a0 = 0 li$t0, 99# $t0 = 99 loop: add $a0, $a0, $t0# $a0 = $a0 + $t0 addi$t0, $t0, -1# $t0 = $t0 - 1 bnez$t0, loop# if ($t0 != zero) branch to loop li$v0, 1# Print the value in $a0 syscall li$v0, 10# Terminate Program Run syscall Three Instruction Word Formats Register Format Immediate Format Jump Format Op-Code Rs Rt Rd Code 6 555 6 Op-Code Rs Rt 16 - Bit Immediate Value 6 55 16 Op-Code 26 Bit Current Segment Address 6 26 A Register Transfer Description of the Control Logic IR = Mem[PC] PC = PC + 4 Decode Instruction Read from Reg. File beqz lw or sw Address = Rs + Offset R-Type If (Rs == 0 ) then PC = PC + Offset sw Memory[Address] = Rt lw Reg. File[Rt] = Memory[Address] Reg. File[Rd] = Rs operation Rt MIPS Instruction SetSee Appendix C in the textbook for a detailed description of every instruction.
  • Arithmetic, Logic, and Shifting Instructions
  • Conditional Branch Instructions
  • Load and Store Instructions
  • Function Call Instructions
  • Pseudo Instructions †
  • Load Address la $s0, table
  • Load Immediateli $v0, 10
  • Move move $t8, $sp
  • Multiplymul $t2, $a0, $a1
  • Dividediv $s1, $v1, $t7
  • Remainderrem $s2, $v1, $t7
  • Negateneg $s0, $s0
  • MIPS Register File Register Naming Convention (See Figure 1.2 on Page 4) $0 : Constant Zero $v0 : Returned values from functions $a0 : Arguments passed to functions $t0 : Temporary registers (functions) $s0 : Saved registers (main program) $sp : Stack Pointer $ra : Return address MIPS Assembly Language Programming
  • Bob Britton, Instructor
  • Lesson #2 Exercises – Chapter 1 1.1Explain the difference between a register and the ALU. 1.2Explain the difference between Assembly Language and Machine Language. 1.3Explain the difference between Cache Memory and the Register File. 1.4Explain the difference between the Instruction Register and the Program Counter. 1.5Explain the difference between a Buss and a control line. 1.6Identify a kitchen appliance that contains a finite state machine. 1.7If a 500 MHz machine takes one-clock cycle to fetch and execute an instruction, then what is the instruction execution rate of the machine? 1.8How many instructions could the above machine execute in one minute? 1.9Let’s suppose we have a 40-year-old computer that has an instruction execution rate of one thousand instructions per second. How long would it take in days, hours, andminutes, to execute the same number of instructions that you derived for the 500 MHz machine? 1.10What is an algorithm? MIPS Assembly Language Programming
  • Bob Britton, Instructor
  • Lesson #3 Pseudocode Using Pseudocode to Document a MIPS Assembly Language Program When documenting an algorithm in a language such as Pascal or 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 Figure 2.1 on page 4 in the textbook. As an assembly language programmer you must take maximum advantage of the processor registers. For example, you may have a value in register $s0 corresponding to speed, a value in register $s1 corresponding to volume, a value in register $s2 corresponding to size, and a value in register $t3 corresponding to count. 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 . 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. Pseudocode for assembly language programs will have the appearance of Pascal or C in terms of control structures and arithmetic expressions, but descriptive 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. 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 $t0 is used to accumulate the sum, and processor register $v0 is used as a loop counter. Use a word processor 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. An Example MIPS Program # Program #1 : (descriptive name) Programmer: YOUR NAME # Due Date : Sep. 13, 2001 Course: CSCI 51A # Last Modified: Sep. 12, 2001Section: 2 ######################################################### # Functional Description: Find the sum of the integers from 1 to N where # N is a value input from the keyboard. ######################################################### # Algorithmic Description in Pseudocode: # main: v0 << value read from the keyboard (syscall 4) # if (v0 < = 0 ) stop # t0 = 0;# t0 is used to accumulate the sum # While (v0 >= 0) { t0 = t0 + v0; v0 = v0 - 1} # Output to monitor syscall(1) << t0; goto main ########################################################## # Register Usage: $t0 is used to accumulate the sum #$v0 the loop counter, counts down to zero ########################################################## .dataprompt:.asciiz "\n\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 **** ".globlmain.textmain:li$v0, 4# system call code for print_strla$a0, prompt# load address of prompt into a0syscall# print the prompt messageli$v0, 5# system call code for read_intsyscall# reads a value of N into v0 blez$v0, done# if ( v0 < = 0 ) go to doneli$t0, 0# clear $t0 to zero loop:add $t0, $t0, $v0# sum of integers in register $t0addi$v0, $v0, -1# summing in reverse orderbnez$v0, loop# branch to loop if $v0 is != zero li$v0, 4# system call code for print_strla$a0, result# load address of message into $a0syscall# print the string li$v0, 1# system call code for print_int move$a0, $t0# a0 = $t0syscall# prints the value in register $a0b main done:li$v0, 4# system call code for print_strla$a0, bye# load address of msg. into $a0syscall# print the stringli$v0, 10# terminate programsyscall # return control to systemMUST HAVE A BLANK LINE AT THE END OF THE TEXT FILE Input/Output System Calls See Appendix A $v0 Service Call CodeArgumentsResults Print_integer1$a0 = integer Print_string4$a0 = &string Read_integer5 $v0= integer Read_string8$a0 = &buffer $a1 = Length of buffer Exit10 Translation of “if – then -- else” if ($t8 < 0) then {$s0 = 0 - $t8; $t1 = $t1 +1} else {$s0 = $t8; $t2 = $t2 + 1} Translation of pseudocode to MIPS assembly language. In MIPS assembly language, anything on a line following the number sign (#) is a comment. Notice how the comments in the code below help to make the connection back to the original pseudocode. bgez$t8, else# if ($t8 is > or = zero) branch to else sub$s0, $zero, $t8 # $s0 gets the negative of $t8 addi$t1, $t1, 1 # increment $t1 by 1 bnext # branch around the else code else: ori$s0, $t8, 0 # $s0 gets a copy of $t8 addi$t2, $t2, 1 # increment $t2 by 1 next: Translation of a “While” statement $v0 = 1 While ($a1 < $a2) do {$t1 = mem[$a1]; $t2 = mem[$a2]; If ($t1 != $t2) go to break; $a1 = $a1 +1; $a2 = $a2 –1;} return break:$v0 = 0 return Here is a translation of the above “while” pseudocode into MIPS assembly language code. li$v0, 1# Load $v0 with the value 1 loop: bgeu$a1, $a2, done# If( $a1 >= $a2) Branch to done lb$t1, 0($a1)# Load a Byte: $t1 = mem[$a1 + 0] lb$t2, 0($a2)# Load a Byte: $t2 = mem[$a2 + 0] bne$t1, $t2, break# If ($t1 != $t2) Branch to break addi$a1, $a1, 1# $a1 = $a1 + 1 addi$a2, $a2, -1# $a2 = $a2 - 1 bloop# Branch to loop break: li$v0, 0# Load $v0 with the value 0 done: Translation of a “for loop” $a0 = 0; For ( $t0 =10; $t0 > 0; $t0 = $t0 -1) do {$a0 = $a0 + $t0} The following is a translation of the above “for-loop” pseudocode to MIPS assembly language code. li$a0, 0# $a0 = 0 li$t0, 10# Initialize loop counter to 10 loop: add$a0, $a0, $t0 addi$t0, $t0, -1# Decrement loop counter bgtz$t0, loop# If ($t0 >0) Branch to loop MIPS Assembly Language Programming
  • Bob Britton, Instructor
  • Lesson #4 Translation of a “switch statement” Pseudocode Description: $s0 = 32; top:cout << “Input a value from 1 to 3” cin >> $v0 switch($v0) { case(1): { $s0 = $s0 << 1; break} case(2): { $s0 = $s0 << 2; break} case(3): { $s0 = $s0 << 3; break} default: goto top ; } cout <<$s0 “switch statement” continued .data .align2 jumptable:.wordtop, case1, case2, case3 prompt:.asciiz "\n\n Input a value N from 1 to 3: " result:.asciiz " The value 32 shifted left by N bits is now = " .text main: li$s0, 32 top: li$v0, 4# Code to print a string la$a0, prompt syscall li$v0, 5# Code to read an integer syscall bltz$v0, exit beqz$v0, top# Default for less than one li$t3, 3 bgt$v0, $t3, top# Default for greater than 3 la$a1, jumptable sll$t0, $v0, 2# Create a word offset add$t1, $a1, $t0# Form a pointer into jumptable lw$t2, 0($t1)# Load an address from jumptable jr$t2# Go to specific case “switch statement” continued case1: sll$s0, $s0, 1 bdone case2: sll$s0, $s0, 2 bdone case3: sll$s0, $s0, 3 done: li$v0, 4# Code to print a string la$a0, result syscall li$v0, 1# Code to print a value move$a0, $s0 syscall bgez$s1, main exit: li$v0, 10 syscall Exercises – Chapter 2 2.1 Using Appendix A, translate each of the following pseudocode expressions into MIPS assembly language: (a)t3 = t4 + t5 – t6; (b)s3 = t2 / (s1 – 54321); (c) sp = sp – 16; (d)cout << t3; (e)cin >> t0; (f)a0 = &array; (g)t8 = Mem(a0); (h)Mem(a0+ 16) = 32768; (i)cout << “Hello World”; (j)If (t0 < 0) then t7 = 0 – t0 else t7 = t0; (k)while ( t0 != 0) { s1 = s1 + t0; t2 = t2 + 4; t0 = Mem(t2) }; (l)for ( t1 = 99; t1 > 0; t1=t1 -1) v0 = v0 + t1; Solutions to Chap 2 Exercises label op-codeRd, Rs, Rt E1a:add $t3, $t4, $t5 sub$t3, $t3, $t6 E1b:addi$s3, $s1, 54321 div$t2, $s3 mflo$s3 E1c:addi$sp, $sp, -16 E1d:move$a0, $t3 li$v0, 1 syscall E1e:li$v0, 5 syscall move$t0, $v0 Solutions to Chap 2 Exercises label op-codeRd, Rs, Rt E1f:la $t0, array lw$a0, 0($t0) E1g: E1h: E1i: E1j: Exercises – Chapter 2          (m)t0 = 2147483647 - 2147483648; (n)     s0 = -1 * s0; (o) s1 = s1 * a0; (p) s2 = srt(s02 + 56) / a3; (q) s3 = s1 - s2 / s3; (r) s4 = s4 * 8; (s)s5 =  * s5; 2.2Analyze the assembly language code that you developed for each of the above pseudocode expressions and calculate the number of clock cycles required to fetch and execute the code corresponding to each expression. (Assume it takes one clock cycle to fetch and execute every instruction except multiply and divide, which require 32 clock cycles and 38 clock cycles respectively.) 2.3Show how the following expression can be evaluated in MIPS assembly language, without modifying the contents of the “s” registers: $t0 = ( $s1 - $s0 / $s2) * $s4 ; Exercises Continued 2.2Analyze the assembly language code that you developed for each of the above pseudocode expressions and calculate the number of clock cycles required to fetch and execute the code corresponding to each expression. (Assume it takes one clock cycle to fetch and execute every instruction except multiply and divide, which require 32 clock cycles and 38 clock cycles respectively.) 2.3Show how the following expression can be evaluated in MIPS assembly language, without modifying the contents of the “s” registers: $t0 = ( $s1 - $s0 / $s2) * $s4 ; MIPS Assembly Language Programming
  • Bob Britton, Instructor
  • Lesson #5 MIPS MIPS Three Instruction Word Formats Register Format Immediate Format Jump Format Op-Code Rs Rt Rd Code 6 555 6 Op-Code Rs Rt 16 - Bit Immediate Value 6 55 16 Op-Code 26 Bit Current Segment Address 6 26 A Register Transfer Description of the Control Logic IR = Mem[PC] PC = PC + 4 Decode Instruction Read from Reg. File beqz lw or sw Address = Rs + Offset R-Type If (Rs == 0 ) then PC = PC + Offset sw Memory[Address] = Rt lw Reg. File[Rt] = Memory[Address] Reg. File[Rd] = Rs operation Rt An Example MIPS Assembly Language Program LabelOp-CodeDest.S1,S2Comments move$a0, $0 # $a0 = 0 li$t0, 99# $t0 = 99 loop: add $a0, $a0, $t0# $a0 = $a0 + $t0 addi$t0, $t0, -1# $t0 = $t0 - 1 bnez$t0, loop# if ($t0 != zero) branch to loop li$v0, 1# Print the value in $a0 syscall li$v0, 10# Terminate Program Run syscall 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 10 496 = 4 x 10 + 9 x 10 + 6 x 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 + 1 x 2 + 0 x 2 + 1x 2 2 A Faster Method ------ Double and Add 00101101 = 45 (1, 2, 5, 11, 22, 45) Conversion of Decimal Numbers to Binary Divide by 2 and record the remainder 45 Remainder1 0 1 1 0 1 221 110 51 21 10 01 Practice - Convert 25 to Binary Divide by 2 and record the remainder 25 Remainder 12 To represent binary values in the positive and negative domains we use the Two’s ComplementNumber System Here is the polynomial expansion of a two’s complement number 8-bit binary number N: N = - d7x2 + d6x2 + d5x2 + d4x2 + d3x2 + d2x2 + d1x2 +d0x2 Notice the Minus sign *** You need to memorize powers of 2 *** 0 7 6 5 4 3 2 1 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 binary value 00011010 is 26 in decimal. To find the value minus 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 complementing the remaining digits to the left: 11100110 The result is the value minus 26 in binary. Binary Arithmetic & Overflow Detectionin theTwo’s Complement Number System Here is an addition example where we assume we are limited to 8 binary digits. 01000100= 68 +00111100= 60 10000000 = -128 Overflow Occurred 0000 1111 0001 0 0010 1110 -1 1 -2 2 0011 1101 3 -3 1100 -4 4 0100 -5 1011 5 -6 0101 6 1010 -7 7 -8 0110 0111 1001 1000 Overflow Binary Arithmeticin theTwo’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. 01000100= 01000100 68 -00111100= +1100010060 00001000= 00001000 = 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: 11111111111111111111111111010101 In Hexadecimal –43 appears as: 0xFFFFFFD5 ############################################### The value 68 as an 8-bit binary number is: 01000100 The value 68 as an 32-bit binary number is: 00000000000000000000000001000100 In Hexadecimal 68 appears as: 0x00000044 The Hexadecimal Number System Here is an example of how we compactly represent binary numbers in hexadecimal: | | | | | 001111001000111101111110 0x 3 C 8 F 7 E DecimalHexBinary 000000 110001 220010 330011 440100 550101 660110 770111 881000 991001 10A1010 11B1011 12C1100 13D1101 14E1110 15F1111 MIPS Assembly Language Programming
  • Bob Britton, Instructor
  • Lesson #6 Chapter 2 Exercises Continued 2.3Show how the following expression can be evaluated in MIPS assembly language, without modifying the contents of the “s” registers: $t0 = ( $s1 - $s0 / $s2) * $s4 ; 2.4The datapath diagram for the MIPS architecture shown in figure 1.1 with only one memory module is referred to as a von Neumann architecture. Most implementations of the MIPS architecture use a Harvard Architecture, where there are two separate memory modules, one for instructions and the the other for data. Draw such a datapath diagram. 2.5Show how the following pseudocode expression can be efficiently evaluated in MIPS assembly language, without modifying the contents of the “s” registers: $t0 = ( $s0 / 8 - 2 * $s1 + $s2 ; Solution to Exercises 2.3 & 2.5 Clock Cycles E2.3div$s0, $s238 mflo$t0 1 sub$t0, $s1, $t0 1 mult$t0, $s432 mflo$t0 1 Total= 73 E2.5sra$t0, $s0, 3 1 sll$t1, $s1, 1 1 sub$t0, $t0, $t1 1 add$t0, $t0, $s2 1 Total= 4 Exercises 3.1Convert the decimal number 35 to an 8-bit binary number. 3.2Convert the decimal number 32 to an 8-bit bina
    We Need Your Support
    Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

    Thanks to everyone for your continued support.

    No, Thanks