Lab 2 : Pseudo Instructions and Memory Access

Lab 2 : Pseudo Instructions and Memory Access
Name:
Sign the following statement:
On my honor, as an Aggie, I have neither given nor received unauthorized aid
on this academic work
1
Objective
The objective of this lab is to make you more familiar with MIPS pseudo instructions as
well as using memory.
2
Pre-requisite
Before starting with this lab, you are required to know what pseudo-instructions are, as
well as how MIPS accesses memory.
3
Basic Memory Access
1. Type the following program into MARS (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
2
. data
msg1 :
. a s c i i z ”A 17 byte message \n”
msg2 :
. a s c i i z ” Another message o f 27 b y t e s \n”
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 :
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
li
$v0 , 10
# system c a l l f o r e x i t
syscall
# we a r e o u t o f h e r e .
(a) What are the assembly instructions for the pseudo instruction la $a0, msg2?
(b) What is the address for each of the following items?
Object
Address
Data Segment
Text Segment
msg1
msg2
num1
num2
num3
num4
(c) Why does msg2 start 18 bytes after msg1 when msg1 is only 17 bytes long?
Computer Architecture and Design, Lab 2
3
(d) Why are there unused bytes between num1, num2, and num3, but num4 start
immediately after num3?
2. Go to the Settings on menu bar and make sure the item Popup dialog for input
syscalls (5,6,7,8,12) is checked. Type the following program into MARS (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
. data
msg1 :
. word 0 : 2 4
. text
. globl main
main :
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
li
$v0 , 10
# system c a l l f o r e x i t
syscall
# we a r e o u t o f h e r e .
Specify the operation performed by the program:
Computer Architecture and Design, Lab 2
4
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 MARS (save your program as lab2d.s):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
. 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 :
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
syscall
l a $a1 , h e x t a b l e
srl
$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
lb $a0 , 0 ( $a2 )
#g e t c h a r a c t e r
l i $v0 , 11
#s y s c a l l f o r p r i n t c h a r
syscall
andi $t0 , $s1 , 0 x f
#g e t l o w e r 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
lb $a0 , 0 ( $a2 )
#g e t c h a r a c t e r
l i $v0 , 11
#s y s c a l l f o r p r i n t s t r
syscall
li
$v0 , 10
# system c a l l f o r e x i t
syscall
# we a r e o u t o f h e r e .
Specify the operation performed by the program:
5. Write a program that reads a number x from the user, and prints the first 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. .
Computer Architecture and Design, Lab 2
4
Deliverables
• Submit completed copy of this lab manual.
• Include the following in a compressed file (.zip format) to your TA:
– The source code for all .s files.
– All log files.
5