a crash course in mips assembly...
TRANSCRIPT
![Page 1: A Crash course in MIPS assembly programmingxyx.se/.../sessions/Session2-MIPS-assembly-programming.pdf · 2017-09-10 · What do we need to know? To program a MIPS, or any processor](https://reader030.vdocuments.mx/reader030/viewer/2022011909/5f73d86c8c8afa5fac1a6ccc/html5/thumbnails/1.jpg)
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
![Page 3: A Crash course in MIPS assembly programmingxyx.se/.../sessions/Session2-MIPS-assembly-programming.pdf · 2017-09-10 · What do we need to know? To program a MIPS, or any processor](https://reader030.vdocuments.mx/reader030/viewer/2022011909/5f73d86c8c8afa5fac1a6ccc/html5/thumbnails/3.jpg)
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
![Page 4: A Crash course in MIPS assembly programmingxyx.se/.../sessions/Session2-MIPS-assembly-programming.pdf · 2017-09-10 · What do we need to know? To program a MIPS, or any processor](https://reader030.vdocuments.mx/reader030/viewer/2022011909/5f73d86c8c8afa5fac1a6ccc/html5/thumbnails/4.jpg)
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
![Page 13: A Crash course in MIPS assembly programmingxyx.se/.../sessions/Session2-MIPS-assembly-programming.pdf · 2017-09-10 · What do we need to know? To program a MIPS, or any processor](https://reader030.vdocuments.mx/reader030/viewer/2022011909/5f73d86c8c8afa5fac1a6ccc/html5/thumbnails/13.jpg)
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
![Page 18: A Crash course in MIPS assembly programmingxyx.se/.../sessions/Session2-MIPS-assembly-programming.pdf · 2017-09-10 · What do we need to know? To program a MIPS, or any processor](https://reader030.vdocuments.mx/reader030/viewer/2022011909/5f73d86c8c8afa5fac1a6ccc/html5/thumbnails/18.jpg)
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
![Page 29: A Crash course in MIPS assembly programmingxyx.se/.../sessions/Session2-MIPS-assembly-programming.pdf · 2017-09-10 · What do we need to know? To program a MIPS, or any processor](https://reader030.vdocuments.mx/reader030/viewer/2022011909/5f73d86c8c8afa5fac1a6ccc/html5/thumbnails/29.jpg)
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
![Page 31: A Crash course in MIPS assembly programmingxyx.se/.../sessions/Session2-MIPS-assembly-programming.pdf · 2017-09-10 · What do we need to know? To program a MIPS, or any processor](https://reader030.vdocuments.mx/reader030/viewer/2022011909/5f73d86c8c8afa5fac1a6ccc/html5/thumbnails/31.jpg)
# 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?
![Page 32: A Crash course in MIPS assembly programmingxyx.se/.../sessions/Session2-MIPS-assembly-programming.pdf · 2017-09-10 · What do we need to know? To program a MIPS, or any processor](https://reader030.vdocuments.mx/reader030/viewer/2022011909/5f73d86c8c8afa5fac1a6ccc/html5/thumbnails/32.jpg)
”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
![Page 42: A Crash course in MIPS assembly programmingxyx.se/.../sessions/Session2-MIPS-assembly-programming.pdf · 2017-09-10 · What do we need to know? To program a MIPS, or any processor](https://reader030.vdocuments.mx/reader030/viewer/2022011909/5f73d86c8c8afa5fac1a6ccc/html5/thumbnails/42.jpg)
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
![Page 48: A Crash course in MIPS assembly programmingxyx.se/.../sessions/Session2-MIPS-assembly-programming.pdf · 2017-09-10 · What do we need to know? To program a MIPS, or any processor](https://reader030.vdocuments.mx/reader030/viewer/2022011909/5f73d86c8c8afa5fac1a6ccc/html5/thumbnails/48.jpg)
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
![Page 49: A Crash course in MIPS assembly programmingxyx.se/.../sessions/Session2-MIPS-assembly-programming.pdf · 2017-09-10 · What do we need to know? To program a MIPS, or any processor](https://reader030.vdocuments.mx/reader030/viewer/2022011909/5f73d86c8c8afa5fac1a6ccc/html5/thumbnails/49.jpg)
Decoding Machine Language
[email protected] 2017 49
A form of ”reverse engineering”, Disassembling machine code