These are slides from Comp 411 Spring 2018

  • Slides: 20
Download presentation
These are slides from Comp 411 (Spring 2018) For Comp 541 projects, we are

These are slides from Comp 411 (Spring 2018) For Comp 541 projects, we are using different address ranges for instruction and data memories. Please be sure to use: l “Default” as Memory Configuration in MARS Settings. data. text 0 x 10010000 0 x 00400000

Computer Organization and Design Assembly & Simulation Montek Singh Feb 14, 2018 Lecture 6

Computer Organization and Design Assembly & Simulation Montek Singh Feb 14, 2018 Lecture 6

Today ã Assembly programming l structure of an assembly program l assembler directives l

Today ã Assembly programming l structure of an assembly program l assembler directives l data and text segments l allocating space for data ã MIPS assembler: MARS l development environment ã A few coding examples l self-study

What is an Assembler? ã A program for writing programs ã Machine Language: l

What is an Assembler? ã A program for writing programs ã Machine Language: l 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 ASM ASSEMBLER Translator program Front panel of a classic PDP 8 e. The toggle switches were used to enter machine language. 01101101 11000110 00101111 10110001. . . STREAM of bits to be loaded into memory Binary Machine Language Assembly: A Symbolic LANGUAGE for representing strings of bits Assembler: A PROGRAM for translating Assembly Source to binary

Assembly Source Language An Assembly SOURCE FILE contains, in symbolic text, values of successive

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 0000. byte 1, 2, 3, 4. byte 5, 6, 7, 8. word 1, 2, 3, 4. asciiz "Comp 411". align 2. word 0 xfeedbeef. text 0 x 00003000 Specifies address for start of data below Four byte values Another four byte values Four word values (each is 4 bytes) A zero (NULL) terminated ASCII string Align to next multiple of 22 A hex-encoded word value Specifies address for start of program text Resulting memory dump: [0 x 0000] [0 x 00000010] [0 x 00000020] 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)

Assembler Syntax ã Assembler DIRECTIVES = Keywords prefixed with ‘. ’ l Control the

Assembler Syntax ã Assembler DIRECTIVES = Keywords prefixed with ‘. ’ l Control the placement and interpretation of bytes in memory. data <addr> Subsequent items are considered data. text <addr> Subsequent items are considered instructions. align N Skip to next address multiple of 2 N l Allocate Storage. byte b 1, b 2, …, bn. half h 1, h 2, …, hn. word w 1, w 2, …, wn. ascii “string”. asciiz “string”. space n l Define scope. globl sym. extern sym size 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 Stores a zero-terminated string Allocates n successive bytes Declares symbol to be visible to other files Sets size of symbol defined in another file (Also makes it directly addressable)

More Assembler Syntax ã Assembler COMMENTS l All text following a ‘#’ (sharp) to

More Assembler Syntax ã Assembler COMMENTS l All text following a ‘#’ (sharp) to the end of the line is ignored ã Assembler LABELS l Labels are symbols that represent memory addresses Ø labels take on the values of the address where they are declared Ø labels can be for data as well as for instructions l Syntax: <start_of_line><label><colon>. data 0 x 80000000 # specifies where data starts item: . word 1 # a data word “int item=1; ”. text 0 x 00010000 start: add $3, $4, $2 sll $3, 8 andi $3, 0 xff beq. . . , start # specifies where code text starts # an instruction label

Even More Assembler Syntax ã Assembler PREDEFINED SYMBOLS l Register names and aliases $0

Even More Assembler Syntax ã Assembler PREDEFINED SYMBOLS l 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 l 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 Ø not all implemented in all MIPS versions l Pseudo-instructions (mnemonics that are not 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 Ø not real MIPS instructions; broken down by assembler into real ones

MARS Settings ã For some of the examples, following Settings apply: (unless specified otherwise)

MARS Settings ã For some of the examples, following Settings apply: (unless specified otherwise) l “Permit extended (pseudo) instructions and formats” Ø is enabled – allows pseudoinstructions to be used – allows variable names to be used (instead of just their addresses) l Memory Configuration is set to Ø "Compact, Data at Address 0” – many of our examples assume that data starts at address 0, and program code starts at address 0 x 3000

A Simple Programming Task ã Add the numbers 0 to 4 … l 0

A Simple Programming Task ã Add the numbers 0 to 4 … l 0 + 1 + 2 + 3 + 4 sum is 10 ã Program in “C”: int i, sum; main() { sum = 0; for (i=0; i<5; i++) sum = sum + i; } ã Now let’s code it in ASSEMBLY

Assembly Code: Sum. asm ã Simple version: put all variables in registers Start addr

Assembly Code: Sum. asm ã Simple version: put all variables in registers Start addr of text is optional A convention borrowed from the C language: the entry point of a program is named “main”. . text 0 x 3000 main: add $8, $0 add $9, $0 loop: add $8, $9 addi $9, 1 slti $10, $9, 5 bne $10, $0, loop end: . . . # sum = 0 # for (i = 0; . . . # # # $8 will have sum $9 will have i sum = sum + i; for (. . . ; i++ for (. . . ; i<5; is $10 true? ie, != 0 need something here to stop! Bookkeeping: 1) Register $8 is allocated as the “sum” variable 2) Register $9 is allocated as the “i” variable We will talk about how to exit a program later

MARS ã MIPS Assembler and Runtime Simulator (MARS) l Java application l Runs on

MARS ã MIPS Assembler and Runtime Simulator (MARS) l Java application l Runs on all platforms l Links on class website l Download it now!

A Slightly More Challenging Program ã Add 5 numbers from a list … l

A Slightly More Challenging Program ã Add 5 numbers from a list … l sum = n 0 + n 1 + n 2 + n 3 + n 4 ã In “C”: int sum, i; 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 code it in assembly

Variable Allocation ã Let’s put variables in memory locations… l … rather than registers

Variable Allocation ã Let’s put variables in memory locations… l … rather than registers ã This time we add the contents of an array Start addr of data is optional . data 0 x 0 sum: . space 4 i: . space 4 a: . word 7, 8, 9, 10, 8 Arrays have to be in memory. Why? ã Note: “. word” also works for an array of words l allows us to initialize a list of sequential words in memory l label represents the address of the first word in the list, or the name of the array Ø does this remind you of how C treats arrays as pointers? ! ã Note: “. space 4” means 4 bytes uninitialized l “. word” needs initial value

The New Code: Sum. Array. asm ã Note the small changes: . text 0

The New Code: Sum. Array. asm ã Note the small changes: . text 0 x 3000 main: sw $0, sum($0) sw $0, i($0) lw $9, i($0) lw $8, sum($0) loop: sll $10, $9, 2 lw $10, a($10) add $8, $10 sw $8, sum($0) addi $9, 1 sw $9, i($0) slti $10, $9, 5 bne $10, $0, loop end: . . . Assembler replaces sum with 0 x 0, i with 0 x 4, and a with 0 x 8 (see previous slide). # # sum = 0; for (i = 0; bring i into $9 bring sum into $8 # # # # convert "i" to word offset load a[i] sum = sum + a[i]; update sum in memory for (. . . ; i++ update i in memory for (. . . ; i<5; # code for exit here

A couple of shortcuts ã Can skip the immediate or register field of lw/sw

A couple of shortcuts ã Can skip the immediate or register field of lw/sw l assumed to be zero Ø lw $8, sum. . . is the same as … lw $8, sum($0) Ø lw $8, ($10). . . is the same as … lw $8, 0($10) l assembler will fill in for you ã Also, can optimize code by eliminating intermediate updates in memory l (next slide)

A couple of shortcuts ã Also, can optimize code by eliminating intermediate updates in

A couple of shortcuts ã Also, can optimize code by eliminating intermediate updates in memory l a good C compiler will do that automatically for you main: add loop: sll lw addi slti bne sw sw end: . . . $9, $0 $8, $0 # i in $9 = 0 # sum in $8 = 0 $10, $9, 2 $10, a($10) $8, $10 $9, 1 $10, $9, 5 $10, $0, loop $8, sum($0) $9, i($0) # # # convert "i" to word offset load a[i] sum = sum + a[i]; for (. . . ; i++ for (. . . ; i<5; # update final sum in memory # update final i in memory # code for exit here

A Coding Challenge ã What is the largest Fibonacci number less than 100? l

A Coding Challenge ã What is the largest Fibonacci number less than 100? l Fibonacci numbers: Fi+1 = Fi + Fi-1 F 0 = 0 F 1 = 1 l 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; } }

MIPS Assembly Code: Fibonacci. asm ã In assembly l let’s use these registers Ø

MIPS Assembly Code: Fibonacci. asm ã In assembly l let’s use these registers Ø x $8 Ø y $9 Ø t $10 . data x: . space 4 y: . space 4. text main: sw addi sw lw while: slti beq add add j endw: sw sw. . . $0, x $9, $0, 1 $9, y $8, x # int x, y; # x = 0; # y = 1; # while (y < 100) { $10, $9, 100 $10, $0, endw $10, $8 $8, $0, $9 $9, $10, $9 while $8, x $9, y # # } int t = x; x = y; y = t + y; # answer is in x # code for exit here

Coming Up… ã Parameterized Programs ã Procedures ã Stacks ã MIPS procedure linkage conventions

Coming Up… ã Parameterized Programs ã Procedures ã Stacks ã MIPS procedure linkage conventions