Adventures in Assembly Land What is an Assembler
Adventures in Assembly Land • What is an Assembler • ASM Directives • ASM Syntax • Intro to MARS • Simple examples Comp 411 – Spring 2009 29 Jan 2009 L 5 – Simulator 1
A Simple Programming Task • Add the numbers 0 to 4 … • 10 = 0 + 1 + 2 + 3 + 4 • In “C”: int i, sum; main() { sum = 0; for (i=0; i<5; i++) sum = sum + i; } • Now let’s code it in ASSEMBLY Comp 411 – Spring 2009 29 Jan 2009 L 5 – Simulator 2
What IS an Assembler? • A program for writing programs • Machine Language: – 1’s and 0’s loaded into memory. (Did anybody ever really do that? ) • Assembly Language: . globl main: subu $sp, 24 sw $ra, 16($sp) li $a 0, 18 li $a 1, 12 li $a 2, 6 jal tak move $a 0, $v 0 Symbolic SOURCE text file Front panel of a classic PDP 8 e. The toggle switches were used to enter machine language. 01101101 11000110 00101111 10110001. . . ASM ASSEMBLER Translator program STREAM of bits to be loaded into memory Binary Machine Language Assembler: 1. A Symbolic LANGUAGE for representing strings of bits 2. A PROGRAM for translating Assembly Source to binary. Comp 411 – Spring 2009 29 Jan 2009 L 5 – Simulator 3
Assembly Source Language An Assembly SOURCE FILE contains, in symbolic text, values of successive bytes to be loaded into memory. . . e. g. . data 0 x 10000000. byte 1, 2, 3, 4. byte 5, 6, 7, 8. word 1, 2, 3, 4. asciiz "Comp 411". align 2. word 0 xfeedbeef • Specifies address where following values are loaded • First four byte values • Second four byte values • Four WORD values (each is 4 bytes) • A string of 9 ASCII bytes (8 + NULL) • Align to next multiple of 4 (2 2) • Hex encoded WORD Value Resulting memory dump: [0 x 10000000] [0 x 10000010] [0 x 10000020] 0 x 04030201 0 x 00000003 0 x 0000 0 x 08070605 0 x 00000004 0 xfeedbeef 0 x 00000001 0 x 706 d 6 f 43 0 x 00000002 0 x 31313420 0 x 0000 Notice the byte ordering. This MIPS is “little-endian” (The least significant byte of a word or half-word has the lowest address) Comp 411 – Spring 2009 29 Jan 2009 L 5 – Simulator 4
Assembler Syntax • Assembler DIRECTIVES (Keywords prefixed with a ‘. ’) • Control the placement and interpretation of bytes in memory. data <addr>. text <addr>. align N Subsequent items are considered data Subsequent items are considered instructions Skip to next address multiple of 2 N • Allocate Storage. byte b 1, b 2, …, bn. half h 1, h 2, …, hn . word w 1, w 2, …, wn. ascii “string” Store a sequence of bytes (8 -bits) Store a sequence of half-words (16 -bits) Store a sequence of words (32 -bits) Stores a sequence of ASCII encoded bytes. asciiz “string”. space n Stores a zero-terminated string Allocates n successive bytes • Define scope. globl sym. extern sym size Comp 411 – Spring 2009 Declares symbol to be visible to other files Sets size of symbol defined in another file (Also makes it DIRECTly addressable) 29 Jan 2009 L 5 – Simulator 5
More Assembler Syntax • Assembler COMMENTS All text following a ‘ #’ (sharp) to the end of the line is ignored • Assembler LABELS • Labels are symbols that represent memory addresses. Labels take on the values of the address where they are declared. Labels declarations appear at the beginning of a line, and are terminated by a colon. Labels can be established for data items as well as instructions… e. g. While having an. data item: start: Comp 411 – Spring 2009 . word. text. word 1 # a data word 0 x 00821820 0 x 00031 a 00 0 x 306300 ff # add # sll # andi 29 Jan 2009 assembler helps, coding like this is still painful. (Don’t actually do this!) $3, $4, $2 $3, 8 $3, 0 xff L 5 – Simulator 6
Our Example: Variable Allocation • Two integer variables (by default 32 bits in MIPS). data. globl sum. globl i sum: . space 4 i: . space 4 • “. data” assembler directive places the following words into the data segment • “. globl” directives make the “sum” and “I” variables visible to all other assembly modules • “. space” directives allocate 4 bytes for each variable Comp 411 – Spring 2009 29 Jan 2009 L 5 – Simulator 7
Even More Assembler Syntax • Assembler PREDEFINED SYMBOLS • Register names and aliases $0 -$31, $zero, $v 0 -$v 1, $a 0 -$a 3, $t 0 -$t 9, $s 0 -$s 7, $at, $k 0 -$k 1, $gp, $sp, $fp, $ra • Assembler MNEMONICS • Symbolic representations of individual instructions add, addu, addiu, subu, andi, ori, xori, nor, lui, sllv, srav, srlv, divu, multu, mfhi, mflo, mthi, mtlo, sltu, sltiu, beq, bgezal, bgtz, blez, bltzal, bltz, bne, j, jalr, jr, lbu, lhu, lwl, lwr, sb, sh, swl, swr, rfe • pseudo-instructions (some mnemonics are not real instructions) abs, mulo, mulou, negu, not, remu, rol, ror, li, seq, sgeu, sgtu, sleu, sne, b, beqz, bgeu, bgtu, bleu, bltu, bnez, la, ld, ulhu, ulw, sd, ush, usw, move, syscall, break, nop Comp 411 – Spring 2009 29 Jan 2009 L 5 – Simulator 8
Actual “Code” • Next we write ASSEMBLY code using the instruction mnemonics A common convention, which originated with the ‘C’ programming language, is for the entry point (starting location) of a program to named “main”. . text. globl main: add $8, $0 add $9, $0 loop: addu $8, $9 addi $9, 1 slti $10, $9, 5 bne $10, $0, loop end: beq $0, end # sum = 0 # for (i = 0; . . . # sum = sum + i; # for (. . . ; i++ # for (. . . ; i<5; Bookkeeping: 1) Register $8 is allocated as the “sum” variable 2) Register $9 is allocated as the “i” variable Comp 411 – Spring 2009 29 Jan 2009 L 5 – Simulator 9
MARS • MIPS Assembler and Runtime Simulator • Java application • Runs on all platforms • Links on class website • (You’ll need to download it for Problem Set #2) Comp 411 – Spring 2009 29 Jan 2009 L 5 – Simulator 10
A Slightly More Challenging Program • Add 5 numbers from a list … • sum = n 0 + n 1 + n 2 + n 3 + n 4 • In “C”: int i, sum; int a[5] = {7, 8, 9, 10, 8}; main() { sum = 0; for (i=0; i<5; i++) sum = sum + a[i]; } • Once more… let’s encode it in assembly Comp 411 – Spring 2009 29 Jan 2009 L 5 – Simulator 11
Variable Allocation • We cheated in our last example. Generally, variables will be allocated to memory locations, rather than registers (Though clever optimization can often avoid it). Arrays have to • This time we add the contents of an array be in memory. Note the use of the “. extern” assembler directive here. MARS *requires*, that “directly” accessed variables are declared as externs. . data. extern sum: i: a: Why? sum 4 i 4 a 20. space 4. word 7, 8, 9, 10, 8 • “. word” allows us to initialize a list of sequential words in memory. The label represents the address of the first word in the list, or the name of the array Comp 411 – Spring 2009 29 Jan 2009 L 5 – Simulator 12
The New Code • Note the small changes: . text. globl main: sw $0, sum sw $0, i lw $9, i lw $8, sum loop: sll $10, $9, 2 lw $10, a($10) addu $8, $10 sw $8, sum addi $9, 1 sw $9, i slti $10, $9, 5 bne $10, $0, loop end: beq $0, end Comp 411 – Spring 2009 # # sum = 0; for (i = 0; allocate register for i allocate register for sum # # # # covert "i" to word offset load a[i] sum = sum + a[i]; update variable in memory for (. . . ; i++ Let’s try it update memory in MARS for (. . . ; i<5; 29 Jan 2009 L 5 – Simulator 13
A Little “Weirdness” The Assembler rewrote one of our instructions. What’s going on? Comp 411 – Spring 2009 29 Jan 2009 L 5 – Simulator 14
A Coding Challenge • What is the largest Fibonacci number less than 100? – Fibonacci numbers: Fi+1 = Fi + F i-1 F 0 = 0 F 1 = 1 – 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … • In “C” int x, y; main() { x = 0; y = 1; while (y < 100) { int t = x; x = y; y = t + y; } } Comp 411 – Spring 2009 29 Jan 2009 L 5 – Simulator 15
MIPS Assembly Code • In assembly . data. extern x 4. extern y 4 x: . space 4 y: . space 4. text. globl main: sw $0, x addi $9, $0, 1 sw $9, y lw $8, x while: slti $10, $9, 100 beq $10, $0, endw add $10, $8 add $8, $0, $9 sw $8, x add $9, $10, $9 sw $9, y beq $0, while endw: beq $0, endw Comp 411 – Spring 2009 29 Jan 2009 # x = 0; # y = 1; # while (y < 100) { # # int t = x; x = y; # y = t + y; # } # answer is x L 5 – Simulator 16
Next Time • Parameterized Programs • Procedures • Stacks • MIPS procedure linkage conventions Comp 411 – Spring 2009 29 Jan 2009 L 5 – Simulator 17
- Slides: 17