0% found this document useful (0 votes)
9 views30 pages

Lec 05

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views30 pages

Lec 05

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

CS222: SPIM and MIPS

Assembly Language Program

Dr. A. Sahu
Dept of Comp. Sc. & Engg.
Indian Institute of Technology Guwahati

1
Outline
• Assemblyy Language
g g Program
g
• Memory Layout of a C program
• Array and Pointer
• SPIM Simulator
• Example
p ALPs
• Assignment 1: Deadline 16 Jan 2011
Register Names and Purpose
Register
Name Usage
number
$zero 0 the constant value 0
$v0-$v1
$ $ 2-3 values for results
$a0-$a3 4-7 arguments
$t0-$t7 8-15 temporaries
$s0-$s7 16-23 saved
$t8-$t9 24-25 more temporaries
$
$gp 28 global
l b l pointer
i t
$sp 29 stack pointer
$fp 30 frame pointer
$ra 31 return address
Instructions to access memory
• Load and store instructions
• Example:

C code: A[8] = h + A[8];

MIPS code: lw $t0, 32($s3)


add $t0
$t0, $s2,
$s2 $t0
sw $t0, 32($s3)
• Stack
– automatic (default), local Stack
– Initialized/uninitialized
• Data
– Global, static, extern Heap
– BSS: Block Started by Symbol
– BBS: Uninitialized Data Seg. BSS
• Code
– program instructions Data
• Heap
– malloc, calloc
Code
int A;
Stack
int B=10;
main(){
{
Heap
int Alocal;
BSS
int *p;
p=(int*)malloc(10);
p=(int )malloc(10); Data
}
Code
Block Started with Symbols: Un initialized
• Segmentation fault: Accessing unallocated area
int A[6];
x
x= A[8]; A[9] k; A[i
A[9]=k; i+3] y;
A[i*i+3]=y;
• Another example:
char *p;
p=(char *)malloc(12*sizeof(char));
strcpy(p,“Helloworld”);/*Seg.Fault*/
( “ ll ld”) /* l */
free(p);
strcpy(p,“AgainWorld”);/*Seg.Fault*/
• How to remove segmentation fault
$gcc test.c
test c
$./aout
segmentation fault

$gcc –g test.c
$gdb ./aout
/aout
( gdb) run
…..
Program received signal SIGSEGV, Segmentation fault.
0x08048480 in main () at test.c:21
21 B[200][100]=5;
[ ][ ] ;
(gdb) backtrace
#0 0x08048480 in main () at test.c:21
Writing a simple loop for ΣiA[i]

s = 0;
i = 0;
L: s = s+A[i];
i++;
if (i<n) goto
L;
Writing a simple loop for ΣiA[i]

s = 0; move $s0, $zero


i = 0; move $t0, $zero
L: s = s+A[i]; L: muli $t1, $t0, 4
add $t1, $t1, $s1 # $s1=&A[0]
lw $t2, 0($t1)
add $s0, $s0, $t2
i++; add
dd $t0,
$ $t0,
$ 1
if (i<n) goto L; blt $t0, $s2, L # $s2 = n
Pseudo instruction blt
blt r1
r1, r2
r2, label

i equivalent
is i l to

slt $at, r1, r2


bne $at
$at, $zero
$zero, label
Overall program

.data
A: .space 400
.text
la $
$s1,, A # lui $
$s1,, Aupper; ori $s1,
$ , $s1,
$ , Alower
li $s2, 100 # ori $s2, $zero, 100
code for input
code for computation
code for output
Improving code: pointer vs. index

s = 0; s = 0;
i = 0; i = 0;
p = &A[0];
L: s = s+A[i]; L: s = s + *p;
i++; p++;
if (i<n) goto L; i++;
if (i<n) goto L;
Improving code: pointer vs. index

s = 0; move $s0, $zero


i = 0; move $t0, $zero
p = &A[0]; la $t1, A
p;
L: s = s + *p; L: lw $
$t2,, 0($t1)
($ )
add $s0, $s0, $t2
p++; addi $t1, $t1, 4
i++; add $t0, $t0, 1
if (i<n) goto L; blt $t0
$t0, $s2
$s2, L # $s2 = n
Improving code further

s = 0; s = 0;
i = 0;
p = &A[0]; p = &A[0];
q = p+100;
p ;
L: s = s + *p; L: s = s + *p;
p++; p++;
i++;
if (i<n) goto L; if (p<q) goto L;
Improving code further

s = 0; move $s0, $zero


p = &A[0]; la $t1, A
q = p+100; addi $s2, $t1, 400
p;
L: s = s + *p; L: lw $
$t2,, 0($t1)
($ )
add $s0, $s0, $t2
p++; addi $t1, $t1, 4
if (p<q) goto L; blt $t1, $s2, L
SPIM: MIPS Simulation
• Simulator: Course Website
• SPIM is a simulator
– reads a MIPS assembly language program.
– simulates each instruction.
– displays values of registers and memory
– supports
s pports breakpoints and single stepping
– provides simple I/O for interacting with
user.
17
SPIM Versions

• SPIM is
i the
h command
d liline version.
i
• XSPIM
S iis X‐Windows
i d version
i ((Unix
i
workstations).
workstations)
• There is also a Windows version
version.

18
SPIM: MIPS Assembly Program
• MIPS assembly language
language.
• Must include a label “main” – this will be
called by the SPIM startup code (allows you to
have command line arguments).
• Can
C iinclude
l d named d memory locations,
l i
constants and string literals in a “data
segment”. ”

19
General Layout
• Data definitions start with .data
data
directive
• Code definition starts with .text
directive
– “text” is the traditional name for the
memory that holds a program.
• Usually have a bunch of subroutine
d fi iti
definitions and
d a “main”.
“ i ”
20
Simple Example
.data # data memory
foo .word 0 # 32 bit variable

.text # program memory


.align
align 2 # word alignment
.globl main # main is global

main:

21
Data Definitions
• You can define variables/constants
with:
–word : defines 32 bit quantities.
byte: defines 8 bit quantities
–byte:
–asciiz: zero‐delimited ascii strings
–.space: allocate some bytes

22
Data Examples
.data
prompt: .asciiz “Hi Dave”
msg: .asciiz
ii “The
“Th answer i
is ”
x: .word 0
y: .word 0
str: .space 100

23
Simple I/O
• SPIM provides some simple I/O using
the “syscall” instruction.
• The specific I/O done depends on
some registers.
registers
–You set $v0 to indicate the operation.
–Parameters in $a0, $a1

24
I/O
/ Functions
$ 0
$v0 F
Function
i P
Parameter
1 print_int
_ $a0 is int
4 print_string $a0 is address of
string
5 read_int returned in $v0
8 read_string $a0 is address of
buffer, $a1 is length

25
Example: Reading an int
addi $v0,$zero,5
syscall

# now $v0 has the integer typed by


# a human in the SPIM console

26
Printing a string
.data
msg: .asciiz “SPIM IS FUN”

main: li $v0,4
la $a0,msg
syscall

27
SPIM subroutines
• The stack is set up for you – just use
$SP
• You can view the stack in the data
window.
window
• main is called as a subroutine (have
it return using jr $ra).

28
Assembly Lang. Program (S=∑i)
.data
Prompt: .asciiz "Please enter a +integer: "
result1: .asciiz “Sum “
.text
# main program with var n: $s0, sum:$s1,i:$s2
main:
li $v0, 4 # issue prompt
la $a0, prompt
syscall
li $v0, 5 # get n from user
syscall
move $s0, $v0
for: blt $
$s0, $
$s2, endf # exit loop if n < i
add $s1, $s1, $s2 # add i to sum
add $s2, $s2, 1 # increment i
b for # continue loop
endf:
df li $ 0 4
$v0, # print
i t “S
“Sum i
is "
la $a0, result1
syscall
li $v0, 1 # print sum
move $a0,
$ 0 $$s1
1
syscall
li $v0, 10 # terminate the program 29
syscall
Assignment :1
• Work: Read a String & print the String in reverse order
• Phase 1::
– Write a C Program array version to do above work
– Write the same C program pointer version
– Optional Phase 1.5: Download MIPS GCC, install, generate
MIPS code from your C Program to view
• Phase 2: Write a MIPS Assembly Language Program
Pointer Version and run on SPIM Simulator
• Deadline: 16.01.2011, Copy Case Lead to F Grade.
• Send both C program, GCC Generated ALPs and Written
ALP to [email protected] with “CS222 Assignment 1” as
subject 30

You might also like