130 likes | 271 Views
SPIM. Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. io.asm. In a nutshell… Displays Hello World! Displays Please enter a number: Reads a number from the user and adds 1
E N D
SPIM Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. Lecture 16
io.asm In a nutshell… Displays Hello World! Displays Please enter a number: Reads a number from the user and adds 1 Displays your number, incremented, is: Displays the incremented number Displays Press enter to end program. Lecture 16
io.asm Lecture 16
io.asm – Data section # 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." comment strings label directive Lecture 16
io.asm – Data section 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 end with a colon. Directives are short hand and perform a specific action Lecture 16
Directives .asciiz "Hello World!\n“ Format - .asciiz str Store the string str in memory and null terminate it. Add ‘\n’ to the end. Lecture 16
io.asm – Text section .text main: # display "hello world" message li $v0,4 # code for print_string la $a0,prompt1 # point $a0 to hello world string syscall # print the string Lecture 16
io.asm – Text section li - Load Immediate instruction Format - li rd, immediate li $v0,4 The (code) value 4 corresponds to a system call to print a string. To use a system call, place the code in $v0 Lecture 16
System Calls Some useful system calls… Call Code Arguments Result Print int 1 $a0 = int Print string 4 $a0 = str Read int 5 $v0 = int Read string 8 $v0 = str Exit 10 Lecture 16
io.asm – Text section la – Load Address instruction la rd, address la $a0,prompt1 Loads the address associated with address into register $a0 (r4). $a0 points to the location of the data containing prompt1. Lecture 16
io.asm – Text section 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. Lecture 16
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. Lecture 16
io.asm – Text section # print out the result li $v0,1 # code for print_int move $a0,$s0 # put number in $a0 syscall # print out the number Code is 1, print an integer. Move the integer from $s0 to $a0 . syscall prints the value stored in $a0. Lecture 16