Computer Organization and Design Assembly & Simulation Montek Singh Mon, Feb 7, 2011 Lecture 5.

Slides:



Advertisements
Similar presentations
CS/COE0447 Computer Organization & Assembly Language
Advertisements

Branches Two branch instructions:
The University of Adelaide, School of Computer Science
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 8 Sept 23 Completion of Ch 2 translating procedure into MIPS role of compilers, assemblers, linking, loading etc. pitfalls, conclusions Chapter.
MIPS Function Continued
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
MIPS Assembly Language Programming
The University of Adelaide, School of Computer Science
Assembly Process. Machine Code Generation Assembling a program entails translating the assembly language into binary machine code This requires more than.
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
Comp Sci instruction encoding 1 Instruction Encoding MIPS machine language Binary encoding of instructions MIPS instruction = 32 bits Three instruction.
1 Lecture 3a: Supplemental Notes for Chapter 3 CS 447 Jason Bakos.
Chapter 3: Instructions
MIPS Assembler Programming
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.
Lecture 5: Procedures. Function call book-keeping in C main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);... } /* really dumb mult function */
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.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: PCSpim Tutorial Engr. Umbreen Sabir Computer Engineering.
IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education.
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 # 1 SPIM & MIPS Programming. SPIM SPIM is a MIPS32 simulator that reads and executes assembly language program written for SPIM. Platform -Unix,
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.
MIPS coding. slt, slti slt $t3, $t1, $t2 – set $t3 to be 1 if $t1 < $t2 ; else clear $t3 to be 0. – “Set Less Than.” slti $t3, $t1, 100 – set $t3 to be.
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
The Assembly Process Computer Organization and Assembly Language: Module 10.
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.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Control Flow and Arrays COE 301 Computer Organization Prof. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University of Petroleum.
Instructor: Prof. Hany H Ammar, LCSEE, WVU
Computer Architecture & Operations I
Prof. Hsien-Hsin Sean Lee
CS2100 Computer Organisation
Computer Organization and Design Assembly & Simulation
Computer Organization and Design Instruction Sets - 2
These are slides from Comp411
32-bit MIPS ISA.
ENGR 3410 – Computer Architecture Mark L. Chang Fall 2006
Computer Organization and Design Instruction Sets
CSCE 212 Chapter 2: Instruction Set Architecture
Adventures in Assembly Land
Appendix A Classifying Instruction Set Architecture
Control Flow and Arrays
MPIS Instructions Functionalities of instructions Instruction format
MIPS coding.
The University of Adelaide, School of Computer Science
Computer Organization and Design Assembly & Simulation
Instruction encoding The ISA defines Format = Encoding
MIPS Instruction Set Architecture
Flow of Control -- Conditional branch instructions
Assembly and Simulation
Flow of Control -- Conditional branch instructions
CS352H Computer Systems Architecture
Generalities for Assembly Language
Adventures in Assembly Land
MIPS Assembly Language Programming Computer Architecture
Adventures in Assembly Land
9/27: Lecture Topics Memory Data transfer instructions
Control Flow and Arrays
Presentation transcript:

Computer Organization and Design Assembly & Simulation Montek Singh Mon, Feb 7, 2011 Lecture 5

Adventures in Assembly Land What is an Assembler ASM Directives ASM Syntax Intro to SPIM/MARS Simple examples

A Simple Programming Task  Add the numbers 0 to 4 … 10 = =  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; }

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?) 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. ASM Symbolic SOURCE text file Binary Machine Language ASSEMBLER Translator program 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 Assembler: 1. A Symbolic LANGUAGE for representing strings of bits 2. 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. Resulting memory dump:.data 0x byte 1, 2, 3, 4.byte 5, 6, 7, 8.word 1, 2, 3, 4.asciiz "Comp 411".align 2.word 0xfeedbeef [0x ] 0x x x x [0x ] 0x x x706d6f43 0x [0x ] 0x xfeedbeef 0x x Notice the byte ordering. This MIPS is “little-endian” (The least significant byte of a word or half-word has the lowest address) Specifies “current” address Four byte values Another four byte values Four word values (each is 4 bytes) A zero-terminated ASCII string Align to next multiple of 2 2 A hex-encoded word value

Assembler Syntax  Assembler DIRECTIVES = Keywords prefixed with ‘.’ Control the placement and interpretation of bytes in memory Control the placement and interpretation of bytes in memory.data Subsequent items are considered data.text Subsequent items are considered instructions.align NSkip to next address multiple of 2 N Allocate Storage Allocate Storage.byte b 1, b 2, …, b n Store a sequence of bytes (8-bits).half h 1, h 2, …, h n Store a sequence of half-words (16-bits).word w 1, w 2, …, w n Store a sequence of words (32-bits).ascii “string”Stores a sequence of ASCII encoded bytes.asciiz “string”Stores a zero-terminated string.space nAllocates n successive bytes Define scope Define scope.globl sym Declares symbol to be visible to other files.extern sym sizeSets size of symbol defined in another file (Also makes it directly addressable)

More Assembler Syntax  Assembler COMMENTS All text following a ‘#’ (sharp) to the end of the line is ignored 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 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. 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..data item:.word 1# a data word.text start:add$3, $4, $2 # an instruction label sll$3, $3, 8 andi$3, $3, 0xff beq...,..., start

Our Example: Variable Allocation  Two integer variables (by default 32 bits in MIPS) Thing to note: “.data” assembler directive places the following words into the data segment “.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 “.globl” directives make the “sum” and “i” variables visible to all other assembly modules “.space” directives allocate 4 bytes for each variable “.space” directives allocate 4 bytes for each variable  in contrast to “.word”, “.space” does not initialize the variables.data.globl sum.globl i sum:.space 4 i:.space 4

Even More Assembler Syntax  Assembler PREDEFINED SYMBOLS Register names and aliases 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 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 pseudo-instructions (mnemonics that are not instructions) 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 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

Actual “Code”  Next we write ASSEMBLY code using the instruction mnemonics.text.globl main main: add $8,$0,$0 # sum = 0 add $9,$0,$0 # for (i = 0;... loop: addu $8,$8,$9 # sum = sum + i; addi $9,$9,1 # for (...;...; i++ slti $10,$9,5 # for (...; i<5; bne $10,$0,loop end: beq $0,$0,end A common convention, which originated with the ‘C’ programming language, is for the entry point (starting location) of a program to named “main”. Bookkeeping: 1) Register $8 is allocated as the “sum” variable 2) Register $9 is allocated as the “i” variable

MIPS Assembly & Simulation  We used to use SPIM (MIPS backwards) a very popular integrated ASSEMBLER, SIMULATOR, and DEBUGGER. a very popular integrated ASSEMBLER, SIMULATOR, and DEBUGGER.  But, now we use MARS cross platform, java-based cross platform, java-based

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

A Slightly More Challenging Program  Add 5 numbers from a list … sum = n 0 + n 1 + n 2 + n 3 + n 4 sum = n 0 + n 1 + n 2 + n 3 + n 4  In “C”:  Once more… let’s encode 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  Variables will be allocated to memory locations… … rather than registers … rather than registers  Though clever optimization can often avoid it  This time we add the contents of an array  “.word” also works for an array of words allows us to initialize a list of sequential words in memory 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 label represents the address of the first word in the list, or the name of the array.data.extern sum 4.extern i 4.extern a 20 sum:.space 4 i:.space 4 a:.word 7,8,9,10,8 Arrays have to be in memory. Why? Note the use of the “.extern” assembler directive here. Most assemblers require that “directly” accessed variables are declared as externs.

The New Code  Note the small changes:.text.globl main main: sw $0,sum # sum = 0; sw $0,i # for (i = 0; lw $9,i # allocate register for i lw $8,sum # allocate register for sum loop: sll $10,$9,2 # covert "i" to word offset lw $10,a($10) # load a[i] addu $8,$8,$10 # sum = sum + a[i]; sw $8,sum # update variable in memory addi $9,$9,1 # for (...;...; i++ sw $9,i # update memory slti $10,$9,5 # for (...; i<5; bne $10,$0,loop end: beq $0,$0,end Try it in MARS

A Little “Weirdness” The Assembler rewrote one of our instructions. What’s going on?

A Coding Challenge  What is the largest Fibonacci number less than 100? Fibonacci numbers: F i+1 = F i + F i-1 F 0 = 0 F 1 = 1 Fibonacci numbers: F i+1 = F i + F i-1 F 0 = 0 F 1 = 1 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … 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  In assembly.data.extern x 4.extern y 4 x:.space 4 y:.space 4.text.globl main 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; sw $9,y beq $0,$0,while # } endw: beq $0,$0, endw # answer is x

Next Time  Parameterized Programs  Procedures  Stacks  MIPS procedure linkage conventions