13
Lecture 16 1 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM

Lecture 161 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM

Embed Size (px)

Citation preview

Page 1: Lecture 161 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM

Lecture 16 1

Lets examine a SPIM program in detail.

io.asm

This program is a simple routine which demonstrates input/output using assembly code.

SPIM

Page 2: Lecture 161 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM

Lecture 16 2

In a nutshell…

Displays Hello World!Displays Please enter a number:Reads a number from the user and adds 1Displays your number, incremented, is:Displays the incremented numberDisplays Press enter to end program.

io.asm

Page 3: Lecture 161 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM

Lecture 16 3

io.asm

Page 4: Lecture 161 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM

Lecture 16 4

# This is a simple program that uses the SPIM simulator

.data

# Constant strings to be output to the terminal

prompt1: .asciiz "Hello World!\n"prompt2: .asciiz "Please enter a number:"result: .asciiz "Your number, incremented, is:"linefeed: .asciiz "\n"enterkey: .asciiz "\nPress enter to end program."

io.asm – Data section

label directive

comment

strings

Page 5: Lecture 161 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM

Lecture 16 5

Single line comments begin with a #.

Identifiers are a sequence of alphanumeric characters, underscores and dots. They do not begin with a digit.

Labels are an identifier for a portion of code, they endwith a colon.

Directives are short hand and perform a specific action

io.asm – Data section

Page 6: Lecture 161 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM

Lecture 16 6

.asciiz "Hello World!\n“

Format - .asciiz str

Store the string str in memory and null terminate it. Add ‘\n’ to the end.

Directives

Page 7: Lecture 161 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM

Lecture 16 7

io.asm – Text section

.text

main:

# display "hello world" messageli $v0,4 # code for print_stringla $a0,prompt1 # point $a0 to hello world stringsyscall # print the string

Page 8: Lecture 161 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM

Lecture 16 8

io.asm – Text section

li - Load Immediate instruction

Format - li rd, immediate

li $v0,4

The (code) value 4 corresponds to a system call toprint a string.

To use a system call, place the code in $v0

Page 9: Lecture 161 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM

Lecture 16 9

System Calls

Some useful system calls…

Call Code Arguments Result

Print int 1 $a0 = intPrint string 4 $a0 = strRead int 5 $v0 = intRead string 8 $v0 = strExit 10

Page 10: Lecture 161 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM

Lecture 16 10

io.asm – Text section

la – Load Address instructionla rd, address

la $a0,prompt1

Loads the address associated with address intoregister $a0 (r4).

$a0 points to the location of the data containingprompt1.

Page 11: Lecture 161 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM

Lecture 16 11

syscall

Invokes a system call which executes a specific routine defined by the code stored in $v0. The data required for the system call is stored in $a0, $a1, $a2, $a3.

io.asm – Text section

Page 12: Lecture 161 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM

Lecture 16 12

io.asm – Text section

# get an integer from the user li $v0,5 # code for read_int syscall # get an int from user --> returned in $v0 move $s0,$v0 # move the resulting int to $s0

addi $s0,$s0,1

Code is 5, read an integer.The resulting read stores it in $v0 (r4).Move the integer to $s0 (r16).Add 1 to the value in $s0.

Page 13: Lecture 161 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM

Lecture 16 13

io.asm – Text section

# print out the resultli $v0,1 # code for print_intmove $a0,$s0 # put number in $a0syscall # print out the number

Code is 1, print an integer.Move the integer from $s0 to $a0 .syscall prints the value stored in $a0.