You are on page 1of 26

Computer Architecture & Organization

Instructions: PCSpim Tutorial Engr. Umbreen Sabir
Computer Engineering Department, University of Engg. & Technology Taxila.

1

19/02/2009

CA&O Lecture 05 by Engr. Umbreen Sabir

Adventures in Assembly Land
    

What is an Assembler ASM Directives ASM Syntax Intro to PCSPIM Simple examples

2

19/02/2009

CA&O Lecture 05 by Engr. Umbreen Sabir

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

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

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

word 0xfeedbeef # Hex encoded WORD Value Resulting memory dump: [0x10000000] 0x04030201 0x08070605 0x00000001 0x00000002 [0x10000010] 0x00000003 0x00000004 0x706d6f43 0x31313420 [0x10000020] 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) 19/02/2009 CA&O Lecture 05 by Engr.Assembly Source Language cont. Umbreen Sabir 6 .    .align 2 # Align to next multiple of 4 (22) .

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

hn Store a sequence of half-words (16-bits) . ….byte b1. ….asciiz “string” Stores a zero-terminated string . w2. h2.ascii “string” Stores a sequence of ASCII encoded bytes .space n Allocates n successive bytes 19/02/2009 CA&O Lecture 05 by Engr. b2. bn Store a sequence of bytes (8-bits) . …. wn Store a sequence of words (32bits) . – Allocate Storage 8 .Assembler Syntax cont. Umbreen Sabir .word w1.half h1.

Umbreen Sabir . – Define scope .globl sym Declares symbol to be visible to other files .Assembler Syntax cont.extern sym size Sets size of symbol defined in another file (Also makes it DIRECTLY addressable) 9 19/02/2009 CA&O Lecture 05 by Engr.

data” assembler directive places the following words into the data segment “.data .space 4 i: .globl sum . Umbreen Sabir    10 .space 4 “.Our Example: Variable Allocation  Two integer variables (by default 32 bits in MIPS) .globl” directives make the “sum” and “i” variables visible to all other assembly modules. “.space” directives allocate 4 bytes for each variable 19/02/2009 CA&O Lecture 05 by Engr.globl i sum: .

.....$0.5 bne $10.$0.$0 add $9. .. . # for (.loop end: beq $0...Actual “Code”  Next we write ASSEMBLY code using the instruction Mnemonics.globl main Main: add $8.end # sum = 0 # for (i = 0. Umbreen Sabir . . 11 19/02/2009 CA&O Lecture 05 by Engr.$0.text 0x80000080 . # sum = sum + i.$8.$9 addi $9.$9. i<5.$9.$0 Loop: addu $8.1 slti $10.$0.... i++ # for (.

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

19/02/2009 CA&O Lecture 05 by Engr. It does so in approximately 4 instructions. Basically it‟s primary job is to branch to the “main” label of your code. w/o a kernel.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. Umbreen Sabir 13 . – We will use a raw version of the machine. later in the course. – You can “single-step” the machine by pressing [F10].

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

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

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

8.10. }  Once more…let‟s encode it in assembly 19/02/2009 CA&O Lecture 05 by Engr. i<5. Umbreen Sabir 17 . sum. i++) sum = sum + a[i]. int a[5] = {7. for (i=0.A Slightly More Challenging Program          Add 5 numbers from a list … sum = n0 + n1 + n2 + n3 + n4 In “C”: int i.8}.9. main() { sum = 0.

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

 “. The label represents the address of the first word in the list.Variable Allocation cont. or the name of the array 19 19/02/2009 CA&O Lecture 05 by Engr.word” allows us to initialize a list of sequential words in memory. Umbreen Sabir .

# for (i = 0.i lw $8.globl main main: sw $0. # allocate register for i # allocate register for sum 20 19/02/2009 CA&O Lecture 05 by Engr.sum sw $0. Umbreen Sabir .The New Code .i lw $9.text 0x80000080 .sum loop: # sum = 0.

$9.. 21 19/02/2009 CA&O Lecture 05 by Engr.loop end: beq $0.end # covert "i" to word offset # load a[i] # sum = sum + a[i].sum addi $9.2 lw $10..$8. # update variable in memory # for (.$9.The New Code sll $10. i++ # update memory # for (.$0.i slti $10.$10 sw $8.$9.. Umbreen Sabir .$0.a($10) addu $8..5 bne $10.1 sw $9. . i<5......

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

21. 1. Umbreen Sabir . 8. 3. 5.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. …  23 19/02/2009 CA&O Lecture 05 by Engr. 34. 13. 2.

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

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

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