19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: PCSpim Tutorial Engr. Umbreen Sabir Computer Engineering.

Slides:



Advertisements
Similar presentations
Goal: Write Programs in Assembly
Advertisements

Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
CS/COE0447 Computer Organization & Assembly Language
Branches Two branch instructions:
Assembly Code Example Selection Sort.
SPIM and MIPS programming
Fall EE 333 Lillevik 333f06-l4 University of Portland School of Engineering Computer Organization Lecture 4 Assembly language programming ALU and.
MIPS Assembly Language Programming
Wannabe Lecturer Alexandre Joly inst.eecs.berkeley.edu/~cs61c-te
1 Computer Architecture MIPS Simulator and Assembly language.
IT253: Computer Organization Lecture 6: Assembly Language and MIPS: Programming Tonga Institute of Higher Education.
The University of Adelaide, School of Computer Science
Assembly Language Working with the CPU.
1 COMS 361 Computer Organization Title: Instructions Date: 9/28/2004 Lecture Number: 10.
Lecture 8: MIPS Instruction Set
Assembly Process. Machine Code Generation Assembling a program entails translating the assembly language into binary machine code This requires more than.
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.
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
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.
CS 300 – Lecture 6 Intro to Computer Architecture / Assembly Language Instructions.
MIPS Instruction Set Advantages
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
MIPS coding. SPIM Some links can be found such as:
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
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.
Computer Organization and Design Assembly & Simulation Montek Singh Mon, Feb 7, 2011 Lecture 5.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Computer Organization and Design Addressing Modes Montek Singh Sep 30, 2015 Lecture 7.
ECE 15B Computer Organization Spring 2011 Dmitri Strukov Partially adapted from Computer Organization and Design, 4 th edition, Patterson and Hennessy,
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 syntax Home Assignment 3 Assigned. Deadline 2016 February 14, Sunday.
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.
CS 312 Computer Architecture & Organization
Computer Architecture & Operations I
MIPS Instruction Set Advantages
CS2100 Computer Organisation
Computer Organization and Design Assembly & Simulation
MIPS Coding Continued.
ACOE301: Computer Architecture II Labs
MIPS assembly syntax Comments
These are slides from Comp411
Adventures in Assembly Land
MIPS coding.
Lecture 4: MIPS Instruction Set
CSCI206 - Computer Organization & Programming
These are slides from Comp411
MIPS Instruction Encoding
MIPS coding.
Computer Organization and Design Assembly & Simulation
MIPS Instruction Encoding
Computer Organization and Design Assembly & Compilation
COMS 361 Computer Organization
COMS 361 Computer Organization
Assembly and Simulation
MIPS Coding Continued.
MIPS coding.
Addressing Modes Don Porter.
Adventures in Assembly Land
MIPS Assembly Language Programming Computer Architecture
Program Assembly.
Adventures in Assembly Land
9/27: Lecture Topics Memory Data transfer instructions
CS 286 Computer Architecture & Organization
Conditional Branching (beq)
Presentation transcript:

19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: PCSpim Tutorial Engr. Umbreen Sabir Computer Engineering Department, University of Engg. & Technology Taxila. 1

Adventures in Assembly Land What is an Assembler ASM Directives ASM Syntax Intro to PCSPIM Simple examples 19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir 2

A Simple Programming Task Add the numbers 0 to 4 … 10 = In “C”:int i, sum; main() {sum = 0; for (i=0; i<5; i++) sum = sum + I; } Now let’s code it in ASSEMBLY 19/02/ CA&O Lecture 05 by Engr. Umbreen Sabir

What IS an Assembler? A program for writing programs Assembler: 1. A Symbolic LANGUAGE for representing strings of bits. 2. A PROGRAM for translating Assembly Source to binary. 19/02/ CA&O Lecture 05 by Engr. Umbreen Sabir

Assembly Source Language An Assembly SOURCE FILE contains, in symbolic text, values of successive bytes to be loaded into memory... e.g..data 0x Specifies address where following values are loaded.byte 1, 2, 3, 4 #First four byte values.byte 5, 6, 7, 8 # Second four byte values.word 1, 2, 3, 4 # 4 WORD values (each is 4 bytes).asciiz "Comp 411“ # A string of 9 ASCII bytes. 19/02/ CA&O Lecture 05 by Engr. Umbreen Sabir

Assembly Source Language cont..align 2 # Align to next multiple of 4 (22).word 0xfeedbeef # Hex encoded WORD Value Resulting memory dump: [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) 19/02/ CA&O Lecture 05 by Engr. Umbreen Sabir

Assembler Syntax Assembler DIRECTIVES (Keywords prefixed with a ‘.’) – Control the placement and interpretation of bytes in memory.data Subsequent items are considered data.text Subsequent items are considered instructions.align N Skip to next address multiple of 2N 19/02/ CA&O Lecture 05 by Engr. Umbreen Sabir

Assembler Syntax cont. – 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 19/02/ CA&O Lecture 05 by Engr. Umbreen Sabir

Assembler Syntax cont. – 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) 19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir 9

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 19/02/ CA&O Lecture 05 by Engr. Umbreen Sabir

Actual “Code” Next we write ASSEMBLY code using the instruction Mnemonics..text 0x 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 19/02/ CA&O Lecture 05 by Engr. Umbreen Sabir

SPIM Simulation Let’s tryout our Example We’ll use the PCSPIM (MIPS backwards) integrated ASSEMBLER, SIMULATOR, and DEBUGGER. 19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir 12

Getting Started SPIMing The following hints will get you started with SPIM – By default a small fragment of code is loaded called the “kernel”. We will discuss the role of this code in detail, later in the course. Basically it’s primary job is to branch to the “main” label of your code. It does so in approximately 4 instructions. – We will use a raw version of the machine, w/o a kernel. – You can “single-step” the machine by pressing [F10]. 19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir 13

Getting Started SPIMing cont. – The results of the execution of each instruction are reflected in the register and memory location values. – Illegal operations result in an “exception” which causes your code to automatically jump back the kernel. – For our purposes now, this will be due to a bug in your program. – Refer to the manual for more fancy usage, such as setting and executing to breakpoints 19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir 14

19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir 15

“RAW” SPIM You’ll need to change the default settings as follows: 19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir 16

A Slightly More Challenging Program Add 5 numbers from a list … sum = n0 + n1 + n2 + n3 + n4 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 19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir 17

Variable Allocation We cheated in our last example. Generally, variables will be allocated to memory locations, rather than registers. This time we add the contents of an array.data.extern sum 4.extern i 4.extern a 20 sum:.space 4 i:.space 4 a:.word 7,8,9,10,8 19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir 18

Variable Allocation cont. “.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 19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir 19

The New Code.text 0x 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: 19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir 20

The New Code 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 19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir 21

19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir 22

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, … 19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir 23

A Coding Challenge In “C” int x, y; main() { x = 0; y = 1; while (y < 100) { int t = x; x = y; y = t + y;} } 19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir 24

Next Lecture and Reminders Next lecture – MIPS ISA Assignment Due – 20 Feb /02/ CA&O Lecture 05 by Engr. Umbreen Sabir

END OF LECTURE 5 19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir 26