Download presentation
Presentation is loading. Please wait.
Published byAlison Gray Modified over 9 years ago
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
2
Lecture 162 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. io.asm
3
Lecture 163 io.asm
4
Lecture 164 # 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 labeldirective comment strings
5
Lecture 165 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 io.asm – Data section
6
Lecture 166.asciiz "Hello World!\n“ Format -.asciiz str Store the string str in memory and null terminate it. Add ‘\n’ to the end. Directives
7
Lecture 167 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
8
Lecture 168 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
9
Lecture 169 System Calls Some useful system calls… CallCodeArguments Result Print int 1$a0 = int Print string 4$a0 = str Read int 5$v0 = int Read string 8$v0 = str Exit 10
10
Lecture 1610 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.
11
Lecture 1611 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
12
Lecture 1612 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.
13
Lecture 1613 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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.