You are on page 1of 5

Lab 2 : Pseudo Instructions and Memory Access

Name:
Your Name

Sign the following statement: On my honor, as an Aggie, I have neither given nor received unauthorized aid on this academic work
Type Name

Objective

The objective of this lab is to make you more familiar with MIPS pseudo instructions as well as using memory.

Pre-requisite

Before starting with this lab, you are required to know what pseudo-instructions are, as well as how MIPS accesses memory.

Basic Memory Access


1. Type the following program into QtSpim (save it as lab2a.s) and use it to answer the questions below 1

Computer Architecture and Design, Lab 2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

. data msg1 : . a s c i i z A 17 byte message msg2 : . a s c i i z Another message o f 27 b y t e s num1 : . byte 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 . text . globl main main : addu $s0 , $ra , $0 #s a v e t h e r e t u r n a d d r e s s l i $v0 , 4 #s y s t e m 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 o f s t r i n g t o p r i n t syscall l a $a0 , msg2 #a d d r e s s o f s t r i n g t o p r i n t syscall lb $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 jr $ra #r e t u r n from main

(a) What are the machine instructions for the pseudo instruction la $a0, msg2?

(b) What is the address for each of the following items? Object Data Segment Text Segment msg1 msg2 num1 num2 num3 num4 Address

Computer Architecture and Design, Lab 2

(c) Why does msg2 start 18 bytes after msg1 when msg1 is only 17 bytes long?

(d) Why are there unused bytes between num1, num2, and num3, but num4 start immediately after num3?

Computer Architecture and Design, Lab 2

2. Type the following program into SPIM (save as lab2b.s):


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

. data msg1 : . word 0 : 2 4 . text . globl main main : addu $s0 , $0 , $ r a #s a v e t h e r e t u r n a d d r e s s l i $v0 , 8 #s y s c a l l f o r r e a d s t r l a $a0 , msg1 #l o a d a d d r e s s o f msg1 t o s t o r e s t r i n g l i $a1 , 100 #msg1 i s 100 b y t e s syscall lb $t0 , 5 ( $a0 ) #l o a d t h e c h a r a c t e r i n t o $ t 0 l i $t1 , a #g e t v a l u e o f a b l t $t0 , $t1 , nomodify #do n o t h i n g i f l e t t e r i s l e s s than a l i $t1 , z #g e t v a l u e o f z bgt $t0 , $t1 , nomodify #do n o t h i n g i f l e t t e r i s g r e a t e r than z addi $t0 , $t0 , 0x20 #encap t h e l e t t e r sb $t0 , 5 ( $a0 ) #s t o r e t h e new l e t t e r nomodify : l i $v0 , 4 #s y s c a l l f o r p r i n t s t r syscall 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

Specify the operation performed by the program:

3. Write a program that reads a string from the user and outputs the number of lowercase letters in the string. Save your program as lab2c.s, and run it to make sure it works correctly. Demonstrate your progress to the TA. . 4. Type the following program into SPIM (save your program as lab2d.s):
1 2 3 4 5 6 7 8 9 10 11 12

. data hextable : . 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 : . text . globl main main : addu $s0 , $0 , $ r a #s a v e t h e r e t u r n a d d r e s s l i $v0 , 5 #s y s c a l l f o r r e a d i n t syscall add $s1 , $v0 , $0 l i $v0 , 4 #s y s c a l l f o r p r i n t s t r l a $a0 , msg1

Computer Architecture and Design, Lab 2

13 14 15 16 17 18 19 20 21 22 23 24 25 26

syscall l a $a1 , h e x t a b l e srl $t0 , $s1 , 4 add $a2 , $a1 , $ t 0 lb $a0 , 0 ( $a2 ) l i $v0 , 11 syscall andi $t0 , $s1 , 0 x f add $a2 , $a1 , $ t 0 lb $a0 , 0 ( $a2 ) l i $v0 , 11 syscall addu $ra , $s0 , $0 jr $ra

#g e t upper 4 b i t s #g e t a d d r e s s i n h e x t a b l e #g e t c h a r a c t e r #s y s c a l l f o r p r i n t c h a r #g e t l o w e r 4 b i t s #g e t a d d r e s s i n h e x t a b l e #g e t c h a r a c t e r #s y s c a l l f o r p r i n t s t r #r e s t o r e r e t u r n a d d r e s s #r e t u r n from main

Specify the operation performed by the program:

5. Write a program that reads a number x from the user, and prints the rst x letters of the alphabet (in lower case). You do not need to check whether the number is positive. Save your program as lab2f.s, and run it to make sure it works correctly. Demonstrate your progress to the TA. .

Deliverables
Submit completed copy of this lab manual. Include the following in a compressed le (.zip format) to your TA: The source code for all .s les. All log les.