Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
A crash course in MIPSassembly programming
Computer Architecture
1DT016 distanceFall 2017
http://xyx.se/1DT016/index.php
Per FoyerMail: [email protected]
[email protected] 2017 1
Where in the machine now?
[email protected] 2017 3
Level 0
Level 1
Level 2
Level 3
Level 4
Level 5
Digital Logic Level
Microprogramminglevel
Conventionalmachine level
Problem-orientedlanguage level
Operating systemmachine level
Assembly languagelevel
addmul: addi $r1, $zero, 2 mul $r1, $r1, 2 jr $ra
int addmul( int t ){ return (t + 2) * 2;}
li $v0, 4syscall
0x24020004 0x0000000c0x03E00008
110110101111010000010110000100010011111010100001
Translation (compiler)
Translation (assembler)
Partial interpretation (OS)
Interpretation(microprogram)
Executed byhardware
What do we need to know?
To program a MIPS, or any processor for that matter, in assembly language we need to know:
•How many registers are there in the CPU?
•How does the memory map look like?
•What assembler instructions does the CPU have?
• Arithmetic, logic, shift and rotate, jump, …
•What do the addressing modes look like?(in what ways can we reference memory)
•What assembler directives are there?
[email protected] 2017 4
MIPS register names
[email protected] 2017 5
MIPS register file
[email protected] 2017 6
MIPS registers and usage convention
[email protected] 2017 7
MIPS memory
[email protected] 2017 8
MIPS memory map
[email protected] 2017 9
MIPS addressing modes
[email protected] 2017 11
Registers and arithmetic
[email protected] 2017 12
Machine language: The binary representation for instructions.
ISA: Instruction Set Architecture
•Fixed size (32-bit) instructions
•Only 3 instruction formats
•Limited sized immediate fields.
•Strict Load and Store architecture. RISC!
•Studying MIPS machine language will also reveal some restrictions in the instruction set architecture (ISA)
The anatomy (ISA) of a MIPS instruction
[email protected] 2017 13
MIPS: register-to-register
[email protected] 2017 14
The I-type format
[email protected] 2017 16
The J-type format
[email protected] 2017 17
The Arithmetic Logic Unit (ALU)
[email protected] 2017 18
ALU
Arithmetic
Logic
A + B
A – B
A * B
A / B
A < B
A == B
A AND B
A OR B
A XOR B
Basic arithmetic and logic
[email protected] 2017 19
Immediate operands
[email protected] 2017 20
Loading and storing bytes
[email protected] 2017 21
Loading and storing words
[email protected] 2017 22
Limitations: Loads and stores
[email protected] 2017 23
Larger constants
[email protected] 2017 24
Memory alignment
[email protected] 2017 25
Data flow in MIPS
[email protected] 2017 26
Function control flow in MIPS
[email protected] 2017 27
Pseudo-instructions
[email protected] 2017 28
Assembler directives.align Align next data item on specified byte boundary (0=byte, 1=half, 2=word, 3=double)
.ascii Store the string in the Data segment but do not add null terminator
.asciiz Store the string in the Data segment and add null terminator
.byte Store the listed value(s) as 8 bit bytes
.data Subsequent items stored in Data segment at next available address
.end_macro End macro definition. See .macro
.eqv Substitute second operand for first. First operand is symbol, second
operand is expression (like #define)
.extern Declare the listed label and byte length to be a global data field
.globl Declare the listed label(s) as global to enable referencing from other files
.half Store the listed value(s) as 16 bit halfwords on halfword boundary
.include Insert the contents of the specified file. Put filename in quotes.
.macro Begin macro definition. See .end_macro
.set Set assembler variables. Currently ignored but included for SPIM compatability
.space Reserve the next specified number of bytes in Data segment
.text Subsequent items (instructions) stored in Text segment at next available address
.word Store the listed value(s) as 32 bit words on word boundary
[email protected] 2017 29
# helloworld.s
#
# Print out "Hello World"
.data
msg: .asciiz "Hello World"
.text
.globl main
main: li $v0, 4 # syscall 4 (print_str)
la $a0, msg # argument: string
syscall # print the string
jr $ra # retrun to caller
Demo: Hello world in Mars
[email protected] 2017 31
After assembling, how do theinstructions look like?
How about the memory map?
”Endianness”
[email protected] 2017 32
First word: ”lleH”…
Big endian
Little endian
Byte 4Byte 3Byte 2Byte 1
Byte 4 Byte 3 Byte 3 Byte 1
Lowest memory address
Branches – Short conditional jumps
[email protected] 2017 33
Larger branch constants
[email protected] 2017 34
Nested functions
[email protected] 2017 35
”Spilling” registers
[email protected] 2017 36
Who saves the registers?
[email protected] 2017 37
The caller?
[email protected] 2017 38
The routine that’s called?
[email protected] 2017 39
…or both?
[email protected] 2017 40
Register spilling convention
[email protected] 2017 41
Demo: stradd.s
Simple program that demonstrates single nested functions following the register spilling convention.
•Uses SPIM / MARS syscalls
•Test setting breakpoints
•Test single backstepping
[email protected] 2017 42
Function calls and stacks
[email protected] 2017 43
Stacks and function calls
[email protected] 2017 44
The MIPS stack
[email protected] 2017 45
Pushing elements
[email protected] 2017 46
Accessing and popping elements
[email protected] 2017 47
Summary: Calls, Registers and Stack
Function (subroutine) calls in MIPS:
•Functions are called using jal, passing arguments in registers $a0-$a3
•Functions place results in registers $v0-$v1 and return using $ra
Assembly programmers must follow many conventions. Nothing prevents a “rogue” program from overwriting registers or stack memory used by some other function.
[email protected] 2017 48
Decoding Machine Language
[email protected] 2017 49
A form of ”reverse engineering”, Disassembling machine code
Finito la musica!
[email protected] 2017 50