ECEN 651: Microprogrammed Control of Digital Systems Department of Electrical and Computer Engineering Texas A&M University Prof. Mi Lu TA: Ehsan Rohani Laboratory Exercise #4 MIPS Assembly and Simulation Objective The objective of lab this week is to familiarize you with the MIPS Instruction Set Architecture (ISA). In order to build a machine, we must understand how to use it. In lab, you will assemble and simulate a few simple programs written in assembly language. You will use the simulator to see the flow of data through the MIPS machine as it executes a program. Additionally, you will have the opportunity to improve upon provided code and will use the simulator to view your improvement. Background The Instruction Set Architecture (ISA) sits at the boundary between hardware and software and provides a well defined set of instructions that both the programmer (or compiler) and the processor understand. Software development using these primitive instructions is referred to as assembly language programming. Today, most software development is performed with a higher level language such as C or C++ and compiled down to assembly code; however, there is still some merit in programming at such a low level. For this class, it is a necessary step, required to strengthen your understanding of the processor itself. For the embedded developer, it may be necessary to write a kernel or a small portion of an algorithm in assembly code for an Operating System (OS) port or efficiency reasons, while for the general programmer, an understanding of what the C or C++ code being written will eventually be translated to will aide in the developer s ability to create efficient programs from the start. For assembly code simulation, we will use the MIPS Assembler and Runtime Simulator (MARS) from Missouri State University. 1
2 Laboratory Exercise #4 MARS provides an intuitive Integrated Development Environment (IDE) for assemble language programming in a portable (written in Java platform). For your use at home, MARS may be downloaded for free at http://courses.missouristate.edu/kenvollmar/mars/ Procedure 1. Introduction to MARS and a very simple assembly language program. (a) Start MARS by first opening up a terminal window, then invoking the following command: >java -jar /homes/grad/atarghe1/ecen651/mars_4_1.jar (b) From within the IDE, select File New to create a new source file. Note the.asm file extension. (c) Enter the following code into the editor within MARS:. t e x t # t e x t s e c t i o n. g l o b l main # c a l l main a d d i $t1, $0, 8 # l o a d immediate v a l u e ( 8 ) i n t o $ t 1 a d d i $t2, $0, 9 # l o a d immediate v a l u e ( 9 ) i n t o $ t 2 add $t3, $t1, $ t 2 # add two numbers i n t o $ t 3 j r $ r a # r e t u r n from main ; r e t u r n a d d r e s s s t o r e d i n $ r a (d) Please note the following MIPS assembly language syntax rules: Text that follows a sharp sign (#) is a comment. A line that starts with a period (.) is an assembler directive. Assembler directives are a way of instructing the assembler how to translate a program but do not produce machine instructions. For example at line 6 in the code below,.globl main defines main as a global variable. Text followed by a colon (:) defines a label. It is similar to defining variables in any highlevel language, for example int var in C-language. (e) Save the file as prog1.asm in your lab4 directory and click on Run Assemble. (f) Select the Execute tab in MARS and record the following things in your lab write-up: The original source with pseudo instructions The actual MIPS instructions they translate to The machine code the MIPS instructions translate to The address where each machine instruction is located 2 ECEN 651
Laboratory Exercise #4 3 (g) Explain what the simple program does. (h) Now select Run Step to single step through the program. Examine the state of the machine provided in the Registers window on the right as you single step through your program. Take note of the PC register and the registers referenced in the program. Record changes you see in the temporary registers (i.e. $t0 through $t9). 2. Assemble and simulate a slightly more complex program with system calls to interact with the user. (a) Create a new file called prog2.asm and type in the following program:. d a t a msg1 :. a s c i i z P l e a s e e n t e r an i n t e g e r number : msg2 :. a s c i i z \ t F i r s t r e s u l t : msg3 :. a s c i i z \ tsecond r e s u l t :. t e x t. g l o b l main # I n s i d e main t h e r e a r e some c a l l s ( ) which w i l l change t h e # v a l u e i n r e g i s t e r $ r a which i n i t i a l l y c o n t a i n s t h e r e t u r n # a d d r e s s from main. This needs t o be saved. addu $s0, $ra, $0 # save $31 i n $16 l i $v0, 4 # system c a l l f o r p r i n t s t r l a $a0, msg1 # a d d r e s s of s t r i n g t o p r i n t # now g e t an i n t e g e r from t h e u s e r l i $v0, 5 # system c a l l f o r r e a d i n t # t h e i n t e g e r p l a c e d i n $v0 # do some c o m p u t a t i o n h e r e with t h e i n t e g e r addu $t0, $v0, $0 # move t h e number i n $v0 t o $ t 0 s l l $t1, $t0, 2 # c o m p u t a t i o n 1, r e s u l t i s i n $ t 1 s r l $t2, $t0, 2 # c o m p u t a t i o n 2, r e s u l t i s i n $ t 2 # p r i n t t h e f i r s t r e s u l t l i $v0, 4 # system c a l l f o r p r i n t s t r l a $a0, msg2 # a d d r e s s of s t r i n g t o p r i n t l i $v0, 1 # system c a l l f o r p r i n t i n t addu $a0, $t1, $0 # move number t o p r i n t i n $a0 ECEN 651 3
4 Laboratory Exercise #4 # p r i n t t h e second r e s u l t l i $v0, 4 # system c a l l f o r p r i n t s t r l a $a0, msg3 # a d d r e s s of s t r i n g t o p r i n t l i $v0, 1 # system c a l l f o r p r i n t i n t addu $a0, $t2, $0 # move number t o p r i n t i n $a0 # r e s t o r e now t h e r e t u r n a d d r e s s i n $ r a and r e t u r n from main addu $ra, $0, $s0 # r e t u r n a d d r e s s back i n $31 j r $ r a # r e t u r n from main $ (b) Using the MIPS reference card listed on the lab website as an aide, examine the code and explain what exactly it is doing. Hint: Look specifically at the SYSCALLS section of the reference card. (c) Verify the operation of the above code in MARS by assembling the code and then selecting Run Go to execute the code to completion without single stepping. Copy the program output displayed in the Run IO window into your lab write-up. (d) Take some time to examine the Text Segment and Data Segments under the Execute tab in MARS. What exactly do these sections contain? (e) Reset the program by selecting Run Reset and now single step through it examining the register contents as you go. Record changes you see in the temporary registers (i.e. $t0 through $t9). (f) Describe in your lab write-up the operation of the PC register. 3. Assemble a program which interacts with memory (i.e. contains loads and stores). (a) Create a new file called prog3.asm and type the following code into that file:. d a t a msg1 :. a s c i i z A 17 b y t e message msg2 :. a s c i i z Another message of 27 b y t e s num1 :. b y t e 45 num2 :. h a l f 654 num3 :. word 0 x c a f e b a b e num4 :. word 0 x f e e d f a c e. t e x t. g l o b l main 4 ECEN 651
Laboratory Exercise #4 5 addu $s0, $ra, $0 # save t h e r e t u r n a d d r e s s l i $v0, 4 # system c a l l f o r p r i n t s t r l a $a0, msg1 # a d d r e s s of s t r i n g t o p r i n t l a $a0, msg2 # a d d r e s s of s t r i n g t o p r i n t l b $t0, num1 # l o a d num1 i n t o $ t 0 l h $t1, num2 # l o a d num2 i n t o $ t 1 lw $t2, num3 # l o a d num3 i n t o $ t 2 lw $t3, num4 # l o a d num4 i n t o $ t 3 addu $ra, $s0, $0 # r e s t o r e t h e r e t u r n a d d r e s s j r $ r a # r e t u r n from main (b) Examine the contents of the Data Segment within MARS. Select the ASCII check box at the bottom of the window and record the start address for the two strings in the program (i.e. msg1 and msg2. (c) Assemble and run the program, single stepping through the instructions. Note the contents of the registers as the code executes. Copy the program output displayed in the Run IO window into your lab write-up. (d) Now type in prog4.asm listed below:. d a t a h e x t a b l e :. a s c i i 0123456789 a b c d e f msg1 :. a s c i i z Your number i n Hex i s :. t e x t. g l o b l main addu $s0, $0, $ r a # save t h e r e t u r n a d d r e s s l i $v0, 5 # f o r r e a d i n t add $s1, $v0, $0 l i $v0, 4 # f o r p r i n t s t r l a $a0, msg1 l a $a1, h e x t a b l e s r l $t0, $s1, 4 # g e t upper 4 b i t s add $a2, $a1, $ t 0 # g e t a d d r e s s i n h e x t a b l e l b $a0, 0( $a2 ) # g e t c h a r a c t e r l i $v0, 11 # f o r p r i n t c h a r ECEN 651 5
6 Laboratory Exercise #4 a n d i $t0, $s1, 0 xf # g e t lower 4 b i t s add $a2, $a1, $ t 0 # g e t a d d r e s s i n h e x t a b l e l b $a0, 0( $a2 ) # g e t c h a r a c t e r l i $v0, 11 # f o r p r i n t s t r addu $ra, $s0, $0 # r e s t o r e r e t u r n a d d r e s s j r $ r a # r e t u r n from main (e) Assemble and run the provided program. In the IO window, enter a number between 1 and 255. (f) Explain the operation of the program in your lab write-up. 4. Assemble and simulate a program with control instruction such as beq and jal. (a) Assemble prog5.asm provided below:. d a t a msg1 :. a s c i i z E n t e r t h e f i r s t number \n msg2 :. a s c i i z E n t e r t h e second number \n msg :. a s c i i z The p r o d u c t i s. t e x t. g l o b l main. g l o b l my mul a d d i $sp, $sp, 8 #make room f o r $ r a and $fp on t h e s t a c k sw $ra, 4( $sp ) # push $ r a sw $fp, 0( $sp ) # push $fp l a $a0, msg1 # l o a d a d d r e s s of msg1 i n t o $a0 l i $v0, 4 # p r i n t msg1 l i $v0, 5 # r e a d i n t add $t0, $v0, $0 # p u t i n $ t 0 l a $a0, msg2 # l o a d a d d r e s s of msg2 i n t o $a0 l i $v0, 4 # p r i n t msg2 l i $v0, 5 6 ECEN 651
Laboratory Exercise #4 7 # r e a d i n t add $a1, $v0, $0 # p u t i n $a1 add $a0, $t0, $0 # p u t f i r s t number i n $a0 add $fp, $sp, $0 # s e t fp t o t o p of s t a c k p r i o r # t o f u n c t i o n c a l l j a l my mul #do mul, r e s u l t i s i n $v0 add $t0, $v0, $0 # save t h e r e s u l t i n $ t 0 l a $a0, msg l i $v0, 4 # p r i n t msg add $a0, $t0, $0 # p u t c o m p u t a t i o n r e s u l t i n $a0 l i $v0, 1 # p r i n t r e s u l t number lw $fp, 0( $sp ) # r e s t o r e ( pop ) $fp lw $ra, 4( $sp ) # r e s t o r e ( pop ) $ r a a d d i $sp, $sp, 8 # a d j u s t $sp j r $ r a # r e t u r n my mul : # m u l t i p l y $a0 with $a1 # does n o t h a n d l e n e g a t i v e $a1! # Note : This i s an i n e f f i c i e n t way t o m u l t i p y! a d d i $sp, $sp, 4 #make room f o r $s0 on t h e s t a c k sw $s0, 0( $sp ) # push $s0 add $s0, $a1, $0 # s e t $s0 e q u a l t o $a1 add $v0, $0, $0 # s e t $v0 t o 0 m u l t l o o p : beq $s0, $0, m u l t e o l add $v0, $v0, $a0 a d d i $s0, $s0, 1 j m u l t l o o p m u l t e o l : lw $s0, 0( $sp ) #pop $s0 j r $ r a (b) Set the Run speed bar at the top of MARS to about 2 inst/sec and then run the program. (c) What does the run speed bar do? ECEN 651 7
8 Laboratory Exercise #4 (d) Watch the program as it executes and summarize its operation in your lab write-up. (e) The method used to perform multiplication is very inefficient. Rewrite the multiplication routine to perform multiplication with the Shift-and-Add method. Compare and contrast the two methods (i.e. the one provided with the one you write) of multiplication in your lab write-up. 1 Deliverables 1. Submit a lab report that captures your efforts in lab. 2. Include all assembly source files with the comments provided. Note: Code submitted without adequate comments will not be graded! 3. Be sure to include all material requested in lab. 4. Answer the following review questions: (a) Explain how the target address in a branch instruction is calculated. How about for a jump instruction? What is the different between jump and jump and link (i.e. jal)? (b) List the registers that must be preserved during the execution of a procedure. Which registers do not have to be preserved? (c) What are the steps necessary to call a procedure? Be sure to include the interaction with the stack and frame pointers. Use prog5.asm as a reference. 8 ECEN 651