Lec 05
Lec 05
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:
$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]
i equivalent
is i l to
.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; 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
• 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
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
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