Mips
Mips
Mips
C Programming
Outline
• MIPS Assembly Language Programming
• C Programming
MIPS Assembly Language Programming
• Assembly Language Program Template
• Defining Data
• Memory Alignment and Byte Ordering
• System Calls
• If, For and While
• Procedures
• Parameter Passing and the Runtime Stack
Program Template
# Title: Filename:
# Author: Date:
# Description:
# Input:
# Output:
################# Data segment #####################
.data
. . .
################# Code segment #####################
.text
.globl main
main: # main program entry
. . .
li $v0, 10 # Exit program
syscall
.DATA, .TEXT, & .GLOBL Directives
• .DATA directive
• Defines the data segment of a program containing data
• The program's variables should be defined under this directive
• Assembler will allocate and initialize the storage of variables
• .TEXT directive
• Defines the code segment of a program containing instructions
• .GLOBL directive
• Declares a symbol as global
• Global symbols can be referenced from other files
• We use this directive to declare main procedure of a program
Layout of a Program in Memory
0x7FFFFFFF
Stack Segment Stack Grows
Downwards
Memory
Addresses
in Hex
Dynamic Area
Data Segment
Static Area
0x10000000
Text Segment
0x04000000
Reserved
0
Next ...
• Assembly Language Program Template
• Defining Data
• Memory Alignment and Byte Ordering
• System Calls
• If, For and While
• Procedures
• Parameter Passing and the Runtime Stack
Data Definition Statement
• Sets aside storage in memory for a variable
• May optionally assign a name (label) to the data
• Syntax:
[name:] directive initializer [, initializer] . . .
var1: .WORD 10
• .ASCIIZ Directive
• Same as .ASCII directive, but adds a NULL char at end of string
• Strings are null-terminated, as in the C programming language
• .SPACE n Directive
• Allocates space of n uninitialized bytes in the data segment
address
• Alignment: address is a multiple of size ...
aligned word
• Word address should be a multiple of 4 12 not aligned
• .ALIGN n directive
• Aligns the next data definition on a 2n byte boundary
Symbol Table
• Assembler builds a symbol table for labels (variables)
• Assembler computes the address of each label in data
segment
• Example Symbol Table
.DATA Label Address
var1: .BYTE 1, 2,'Z' var1 0x10010000
str1: .ASCIIZ "My String\n"
str1 0x10010003
var2: .WORD 0x12345678
var2 0x10010010
.ALIGN 3
var3: .HALF 1000 var3 0x10010018
str1
var1
0x10010000 1 2 'Z' 'M' 'y' ' ' 'S' 't' 'r' 'i' 'n' 'g' '\n' 0 0 0 Unused
0x10010010 0x12345678 0 0 0 0 1000 0 0 0 0 0 0
var2 (aligned) Unused var3 (address is multiple of 8)
Byte Ordering and Endianness
• Processors can order bytes within a word in two ways
• Little Endian Byte Ordering
• Memory address = Address of least significant byte
• Example: Intel IA-32, Alpha
MSB LSB address a a+1 a+2 a+3
Byte 3 Byte 2 Byte 1 Byte 0 . . . Byte 0 Byte 1 Byte 2 Byte 3 . . .
32-bit Register Memory
• Big Endian Byte Ordering
• Memory address = Address of most significant byte
• Example: SPARC, PA-RISC
MSB LSB address a a+1 a+2 a+3
Byte 3 Byte 2 Byte 1 Byte 0 . . . Byte 3 Byte 2 Byte 1 Byte 0 . . .
32-bit Register Memory
• MIPS can operate with both byte orderings
Next ...
• Assembly Language Program Template
• Defining Data
• Memory Alignment and Byte Ordering
• System Calls
• If, For and While
• Procedures
• Parameter Passing and the Runtime Stack
System Calls
• Programs do input/output through system calls
• MIPS provides a special syscall instruction
• To obtain services from the operating system
• Many services are provided in the SPIM and MARS simulators
• Using the syscall system services
• Load the service number in register $v0
• Load argument values, if any, in registers $a0, $a1, etc.
• Issue the syscall instruction
• Retrieve return values, if any, from result registers
Syscall Services
Service $v0 Arguments / Result
Print Integer 1 $a0 = integer value to print
Print Float 2 $f12 = float value to print
Print Double 3 $f12 = double value to print
Print String 4 $a0 = address of null-terminated string
Read Integer 5 $v0 = integer read
Read Float 6 $f0 = float read
Read Double 7 $f0 = double read
Read String 8 $a0 = address of input buffer
$a1 = maximum number of characters to read
Exit Program 10
Print Char 11 $a0 = character to print
Read Char 12 $a0 = character read
Reading and Printing an Integer
################# Code segment #####################
.text
.globl main
main: # main program entry
li $v0, 5 # Read integer
syscall # $v0 = value read
Let’s assume that the values of variables var1 and var2 are in
registers $t0 and $t1. Then, this piece of C code would be
translated as:
swap:
0040003C sll $8, $5, 2 sll $t0,$a1,2 $31 0x00400030
00400040 add $8, $8, $4 add $t0,$t0,$a0
00400044 lw $9, 0($8) lw $t1,0($t0)
00400048 lw $10,4($8) lw $t2,4($t0) Register $31
0040004C sw $10,0($8) sw $t2,0($t0) is the return
00400050 sw $9, 4($8) sw $t1,4($t0) address register
00400054 jr $31 jr $ra
Instructions for Procedures
JAL (Jump-and-Link) used as the call instruction
Save return address in $ra = PC+4 and jump to procedure
Register $ra = $31 is used by JAL as the return address
JR (Jump Register) used to return from a procedure
Jump to instruction whose address is in register Rs (PC = Rs)
JALR (Jump-and-Link Register)
Save return address in Rd = PC+4, and
Jump to procedure whose address is in register Rs (PC = Rs)
Can be used to call methods (addresses known only at runtime)
Instruction Meaning Format
jal label $31=PC+4, jump op6 = 3 imm26
jr Rs PC = Rs op6 = 0 rs5 0 0 0 8
jalr Rd, Rs Rd=PC+4, PC=Rs op6 = 0 rs5 0 rd5 0 9
Next ...
• Assembly Language Program Template
• Defining Data
• Memory Alignment and Byte Ordering
• System Calls
• If, For and While
• Procedures
• Parameter Passing and the Runtime Stack
Parameter Passing
• Parameter passing in assembly language is different
• More complicated than that used in a high-level language
• In assembly language
• Place all required parameters in an accessible storage area
• Then call the procedure
• Two types of storage areas used
• Registers: general-purpose registers are used (register method)
• Memory: stack is used (stack method)
• Two common mechanisms of parameter passing
• Pass-by-value: parameter value is passed
• Pass-by-reference: address of parameter is passed
Parameter Passing – cont'd
• By convention, registers are used for parameter passing
• $a0 = $4 .. $a3 = $7 are used for passing arguments
• $v0 = $2 .. $v1 = $3 are used for result values
• Additional arguments/results can be placed on the stack
• Runtime stack is also needed to …
• Store variables / data structures when they cannot fit in registers
• Save and restore registers across procedure calls
• Implement recursion
• Runtime stack is implemented via software convention
• The stack pointer $sp = $29 (points to top of stack)
• The frame pointer $fp = $30 (points to a procedure frame)
Stack Frame
• Stack frame is the segment of the stack containing …
• Saved arguments, registers, and local data structures (if any)
• Called also the activation frame or activation record
• Frames are pushed and popped by adjusting …
• Stack pointer $sp = $29 and Frame pointer $fp = $30
• Decrement $sp to allocate stack frame, and increment to free
$fp
Stack Stack Stack arguments
f calls g
$fp $fp
Frame f() Frame f() Frame f() saved $ra
g returns
$sp
$fp
$sp saved
↓ Frame g() ↑ registers
$sp
local data
stack grows allocate free stack structures
downwards stack frame frame or variables
$sp
Preserving Registers
• Need to preserve registers across a procedure call
• Stack can be used to preserve register values
• Which registers should be saved?
• Registers modified by the called procedure, and
• Still used by the calling procedure
• Who should preserve the registers?
• Called Procedure: preferred method for modular code
• Register preservation is done inside the called procedure
• By convention, registers $s0, $s1, …, $s7 should be preserved
• Also, registers $sp, $fp, and $ra should also be preserved
Selection Sort
Array Array Array Array
first first first first
last
last last last value last max value max value
Unsorted Locate Swap Max Decrement
• Example Max with Last Last
• For example:
• build search data structures (hash tables)
• construct digital signatures
• ensure data integrity
• build cryptosystem
• quickly perform file comparisons
• store passwords securely
Brief Overview of Hashing
• A hash function is one that takes an arbitrary length input (e.g. a
message) and produces a fixed-length output, or digest
Hash Fun
”Hello World!” 0x0893fe98