90 likes | 207 Views
This guide explores the layout of a MIPS program, focusing on data and code segments. It explains how to define a data segment using `.data` to create strings and arrays, while the code segment is marked by `.text`. Key syscalls for input and output operations are discussed, such as printing integers, strings, and reading characters. An example demonstrates string reversal in MIPS, illustrating stack operations and syscall usage. This foundational knowledge is essential for programming in MIPS assembly language.
E N D
CDA 3100 Recitation Week 6
Layout of MIPS program • Data segment • Specified by: .data • Code segment • Specified by: .text .globlmain main:
Data Segment Examples • Create strings of text msg: .asciiz “hello world” • Create arrays of data A: .word 0,1,2,3,4,5,6,7,8,9 • Allocate a chunk of memory empty: .space 400
Syscalls • Performs a variable service dependent upon the values in various registers • $v0 specifies the operation • $a0 (typically) specifies the argument • Other places arguments can come from: • $a1, $a2, $a3 – for operations with multiple args • $f12 – for floating point number calls • Sample call: li $v0, 1 li $a0, 20 syscall
Common Syscalls Examples • Print integer • $v0 = 1, $a0 = integer to print • Print float • $v0 = 2, $f12 = float to print • Print string • $v0 = 4, $a0 = string_address • Print character • $v0 = 11, $a0 = character to print • Read integer • $v0 = 5; after call, $v0 = integer read • Read character • $v0 = 12; after call, $v0 = character read • Quit execution • $v0 = 10 • More examples: • http://courses.missouristate.edu/kenvollmar/mars/help/syscallhelp.html
Reverse a String (Part 1) .data msg: .asciiz "hello world“ endl: .asciiz "\n" .text .globl main main: # Initialize stack and load ‘msg’ addi$sp,$sp,-1 sb$0,0($sp) la $t1, msg
Reverse a String (Part 2) L0: # Load ‘msg’ string onto stack lb$t0,0($t1) beq$t0,$0, L1 addi$sp,$sp,-1 sb$t0,0($sp) addi$t1,$t1,1 j L0 L1: # Reload ‘msg’ pointer la $t1,msg
Reverse a String (Part 3) L2: # Replace string at ‘msg’ lb$t0,0($sp) addi$sp,$sp,1 sb$t0,0($t1) beq$t0, $0, L3 addi$t1,$t1,1 j L2
Reverse a String (Part 4) L3: # Print messages and quit la $a0,msg # Load string located at ‘msg’ li $v0,4 syscall la $a0,endl # Load string located at ‘endl’ li $v0,4 syscall li $v0,10 # Exit syscall