These are slides from Comp411

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

CS/COE0447 Computer Organization & Assembly Language
Branches Two branch instructions:
Deeper Assembly: Addressing, Conditions, Branching, and Loops
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
MIPS Assembly Language Programming
The University of Adelaide, School of Computer Science
L5 – Addressing Modes 1 Comp 411 – Spring /29/08 Operands and Addressing Modes Where is the data? Addresses as data Names and Values Indirection.
1 Lecture 3a: Supplemental Notes for Chapter 3 CS 447 Jason Bakos.
MIPS Assembler Programming
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
MIPS on FLEET Amir Kamil. Goals Run most MIPS assembly code on FLEET  Attempt to duplicate level of support in SPIM interpreter  MIPS assembly translated.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
L6 – Simulator 1 Comp 411 – Fall /12/06 Adventures in Assembly Land What is an Assembler ASM Directives ASM Syntax Intro to SPIM Simple examples.
L5 – Simulator 1 Comp 411 – Fall /16/09 Adventures in Assembly Land What is an Assembler ASM Directives ASM Syntax Intro to SPIM/MARS Simple examples.
VHDL Synthesis of a MIPS-32 Processor Bryan Allen Dave Chandler Nate Ransom.
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: PCSpim Tutorial Engr. Umbreen Sabir Computer Engineering.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
Computer Organization and Design Assembly & Simulation
Lecture 4: MIPS Instruction Set
MIPS coding. Review Shifting – Shift Left Logical (sll) – Shift Right Logical (srl) – Moves all of the bits to the left/right and fills in gap with 0’s.
Computer Organization and Design Assembly & Simulation Montek Singh Mon, Feb 7, 2011 Lecture 5.
Computer Organization and Design Addressing Modes Montek Singh Wed, Sep 19, 2012 Lecture 6.
Computer Organization and Design Addressing Modes Montek Singh Sep 30, 2015 Lecture 7.
MIPS Assembly Language Chapter 13 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
EEL5708/Bölöni Lec 3.1 Fall 2006 Sept 1, 2006 Lotzi Bölöni EEL 5708 High Performance Computer Architecture Lecture 3 Review: Instruction Sets.
MIPS Assembly Language Chapter 15 S. Dandamudi To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
CDA 3101 Spring 2016 Introduction to Computer Organization
Computer Organization and Design Assembly & Simulation Montek Singh Wed, Sep 26, 2012 Lecture 7.
Computer Organization and Design Assembly & Simulation Montek Singh Feb 22, 2016 Lecture 6.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
Computer Architecture & Operations I
Prof. Hsien-Hsin Sean Lee
Lecture 6: Assembly Programs
CS2100 Computer Organisation
Computer Organization and Design Assembly & Simulation
Computer Organization and Design Instruction Sets - 2
Lecture 4: MIPS Instruction Set
Computer Organization and Design Instruction Sets - 2
32-bit MIPS ISA.
ENGR 3410 – Computer Architecture Mark L. Chang Fall 2006
Computer Organization and Design Instruction Sets
Adventures in Assembly Land
Lecture 4: MIPS Instruction Set
Control Flow and Arrays
MPIS Instructions Functionalities of instructions Instruction format
Computer Organization and Design Addressing Modes
ECE232: Hardware Organization and Design
MIPS coding.
Operands and Addressing Modes
The University of Adelaide, School of Computer Science
Computer Organization and Design Assembly & Simulation
Instruction encoding The ISA defines Format = Encoding
Computer Organization and Design Assembly & Compilation
Flow of Control -- Conditional branch instructions
Assembly and Simulation
Flow of Control -- Conditional branch instructions
CS352H Computer Systems Architecture
Generalities for Assembly Language
Addressing Modes Don Porter.
Adventures in Assembly Land
MIPS Assembly Language Programming Computer Architecture
Computer Organization and Design Addressing Modes
Adventures in Assembly Land
Operands and Addressing Modes
9/27: Lecture Topics Memory Data transfer instructions
Control Flow and Arrays
Presentation transcript:

These are slides from Comp411 For Comp541 projects, we are using different address ranges for instruction and data memories. Please be sure to use: “Default” as Memory Configuration in MARS Settings .data 0x10010000 .text 0x00400000

Computer Organization and Design Assembly & Simulation Montek Singh Oct 2, 2017 Lecture 6

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

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: Front panel of a classic PDP8e. The toggle switches were used to enter machine language. STREAM of bits to be loaded into memory .globl main main: subu $sp, $sp, 24 sw $ra, 16($sp) li $a0, 18 li $a1, 12 li $a2, 6 jal tak move $a0, $v0 ASM 01101101 11000110 00101111 10110001 ..... Symbolic SOURCE text file ASSEMBLER Translator program 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 bytes to be loaded into memory... e.g. .data 0x00000000 .byte 1, 2, 3, 4 .byte 5, 6, 7, 8 .word 1, 2, 3, 4 .asciiz "Comp 411" .align 2 .word 0xfeedbeef .text 0x00003000 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: [0x00000000] 0x04030201 0x08070605 0x00000001 0x00000002 [0x00000010] 0x00000003 0x00000004 0x706d6f43 0x31313420 [0x00000020] 0x00000000 0xfeedbeef 0x00000000 0x00000000 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 ‘.’ 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 2N Allocate Storage .byte b1, b2, …, bn Store a sequence of bytes (8-bits) .half h1, h2, …, hn Store a sequence of half-words (16-bits) .word w1, w2, …, wn Store a sequence of words (32-bits) .ascii “string” Stores a sequence of ASCII encoded bytes .asciiz “string” Stores a zero-terminated string .space n Allocates n successive bytes Define scope .globl sym Declares symbol to be visible to other files .extern sym size Sets size of symbol defined in another file (Also makes it directly addressable)

More Assembler Syntax Assembler COMMENTS Assembler LABELS 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 can be for data as well as for instructions Syntax: <start_of_line><label><colon> .data 0x80000000 item: .word 1 # a data word .text 0x00010000 start: add $3, $4, $2 # an instruction label sll $3, $3, 8 andi $3, $3, 0xff beq ..., ..., start

Even More Assembler Syntax Assembler PREDEFINED SYMBOLS Register names and aliases $0-$31, $zero, $v0-$v1, $a0-$a3, $t0-$t9, $s0-$s7, $at, $k0-$k1, $gp, $sp, $fp, $ra Assembler MNEMONICS Symbolic representations of individual instructions add, addu, addi, addiu, sub, subu, and, andi, or, ori, xor, xori, nor, lui, sll, sllv, sra, srav, srl, srlv, div, divu, mult, multu, mfhi, mflo, mthi, mtlo, slt, sltu, slti, sltiu, beq, bgez, bgezal, bgtz, blez, bltzal, bltz, bne, j, jal, jalr, jr, lb, lbu, lh, lhu, lw, lwl, lwr, sb, sh, sw, swl, swr, rfe not all implemented in all MIPS versions Pseudo-instructions (mnemonics that are not instructions) abs, mul, mulo, mulou, neg, negu, not, rem, remu, rol, ror, li, seq, sge, sgeu, sgt, sgtu, sle, sleu, sne, b, beqz, bge, bgeu, bgt, bgtu, ble, bleu, blt, bltu, bnez, la, ld, ulh, 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 example, following Settings apply: (unless specified otherwise) “Permit extended (pseudo) instructions and formats” is enabled allows pseudoinstructions to be used allows variable names to be used (instead of just their addresses) 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 0x3000

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

Assembly Code: Sum.asm Next we write ASSEMBLY code using instr 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 0x3000 main: add $8,$0,$0 # sum = 0 add $9,$0,$0 # for (i = 0; ... loop: add $8,$8,$9 # sum = sum + i; addi $9,$9,1 # for (...; ...; i++ slti $10,$9,5 # for (...; i<5; bne $10,$0,loop end: ... # need something here to stop! $8 will have sum $9 will have i 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) Java application Runs on all platforms Links on class website Download it now!

A Slightly More Challenging Program Add 5 numbers from a list … sum = n0 + n1 + n2 + n3 + n4 In “C”: Once more… let’s code it in assembly int i, sum; int a[5] = {7,8,9,10,8}; main() { sum = 0; for (i=0; i<5; i++) sum = sum + a[i]; }

Variable Allocation Let’s put variables in memory locations… … rather than registers This time we add the contents of an array Note: “.word” also works for an array of words allows us to initialize a list of sequential words in memory 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 “.word” needs initial value Arrays have to be in memory. Why? .data 0x0 sum: .space 4 i: .space 4 a: .word 7,8,9,10,8

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

A couple of shortcuts Can skip the immediate or register field of lw/sw 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) assembler will fill in for you Also, can optimize code by eliminating intermediate updates in memory (next slide)

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

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

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

Assembly Coding Templates For common C program fragments

Conditionals: if-else There are little tricks that come into play when compiling conditional code blocks. For instance, the statement: if (y > 32) { x = x + 1; } compiles to: lw $24, y($0) ori $15, $0, 32 slt $1, $15, $24 beq $1, $0, Lendif lw $24, x($0) addi $24, $24, 1 sw $24, x($0) Lendif: C code: if (expr) { STUFF } MIPS assembly: (compute expr in $rx) beq $rx, $0, Lendif (compile STUFF) Lendif: C code: if (expr) { STUFF1 } else { STUFF2 } MIPS assembly: (compute expr in $rx) beq $rx, $0, Lelse (compile STUFF1) j Lendif Lelse: (compile STUFF2) Lendif:

Loops: while MIPS assembly: Lwhile: Lendw: Alternate MIPS assembly: C code: while (expr) { STUFF } MIPS assembly: Lwhile: (compute expr in $rx) beq $rx,$0,Lendw (compile STUFF) j Lwhile Lendw: Alternate MIPS assembly: j Ltest Lwhile: (compile STUFF) Ltest: (compute expr in $rx) bne $rx,$0,Lwhile Lendw: Compilers spend a lot of time optimizing in and around loops - moving all possible computations outside of loops - unrolling loops to reduce branching overhead - simplifying expressions that depend on “loop variables”

Loops: for Most high-level languages provide loop constructs that establish and update an iteration variable, which is used to control the loop’s behavior MIPS assembly: .data sum: .word 0x0 data: .word 0x1, 0x2, 0x3, 0x4, 0x5 .word 0x6, 0x7, 0x8, 0x9, 0xa .text add $30,$0,$0 Lfor: lw $24,sum($0) sll $15,$30,2 lw $15,data($15) add $24,$24,$15 sw $24,sum($0) addi $30,$30,1 slti $24,$30,10 bne $24,$0,Lfor Lendfor: C code: int sum = 0; int data[10] = {1,2,3,4,5,6,7,8,9,10}; int i; for (i=0; i<10; i++) { sum += data[i] }

Coming Up… Parameterized Programs Procedures Stacks MIPS procedure linkage conventions