0% found this document useful (0 votes)
63 views

Computer Architecture & Organization: Instructions: Pcspim Tutorial

This document contains a lecture on computer architecture and organization given by Engr. Umbreen Sabir. It discusses assemblers, assembly syntax, and provides examples of coding simple programs in MIPS assembly language and simulating them using PCSpim. It also presents coding challenges, such as calculating the sum of numbers in an array and finding the largest Fibonacci number less than 100. The next lecture will cover the MIPS instruction set architecture.

Uploaded by

Ali Ahmad
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Computer Architecture & Organization: Instructions: Pcspim Tutorial

This document contains a lecture on computer architecture and organization given by Engr. Umbreen Sabir. It discusses assemblers, assembly syntax, and provides examples of coding simple programs in MIPS assembly language and simulating them using PCSpim. It also presents coding challenges, such as calculating the sum of numbers in an array and finding the largest Fibonacci number less than 100. The next lecture will cover the MIPS instruction set architecture.

Uploaded by

Ali Ahmad
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Computer Architecture & Organization

Instructions: PCSpim Tutorial Engr. Umbreen Sabir


Computer Engineering Department, University of Engg. & Technology Taxila.

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

19/02/2009

CA&O Lecture 05 by Engr. Umbreen Sabir

A Simple Programming Task


Add the numbers 0 to 4 10 = 0 + 1 + 2 + 3 + 4 In C: int i, sum; main() { sum = 0; for (i=0; i<5; i++) sum = sum + I; } Now lets code it in ASSEMBLY
19/02/2009 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/2009

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 0x10000000 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/2009 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:

[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. Umbreen Sabir

Assembler Syntax

Assembler DIRECTIVES (Keywords prefixed with a .)

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

19/02/2009

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 (32bits) .ascii string Stores a sequence of ASCII encoded bytes .asciiz string Stores a zero-terminated string .space n Allocates n successive bytes
19/02/2009 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)
9

19/02/2009

CA&O Lecture 05 by Engr. Umbreen Sabir

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

10

Actual Code

Next we write ASSEMBLY code using the instruction Mnemonics.

.text 0x80000080 .globl main Main: add $8,$0,$0 add $9,$0,$0 Loop: addu $8,$8,$9 addi $9,$9,1 slti $10,$9,5 bne $10,$0,loop end: beq $0,$0,end

# sum = 0 # for (i = 0; ... # sum = sum + i; # for (...; ...; i++ # for (...; i<5;

11

19/02/2009

CA&O Lecture 05 by Engr. Umbreen Sabir

SPIM Simulation

Lets tryout our Example Well use the PCSPIM (MIPS backwards) integrated ASSEMBLER, SIMULATOR, and DEBUGGER.

12

19/02/2009

CA&O Lecture 05 by Engr. Umbreen Sabir

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

14

15

19/02/2009

CA&O Lecture 05 by Engr. Umbreen Sabir

RAW SPIM

Youll need to change the default settings as follows:

16

19/02/2009

CA&O Lecture 05 by Engr. Umbreen Sabir

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 morelets encode it in assembly


19/02/2009 CA&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/2009 CA&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

19/02/2009

CA&O Lecture 05 by Engr. Umbreen Sabir

The New Code


.text 0x80000080 .globl main main: sw $0,sum sw $0,i lw $9,i lw $8,sum loop:

# sum = 0; # for (i = 0; # allocate register for i # allocate register for sum

20

19/02/2009

CA&O Lecture 05 by Engr. Umbreen Sabir

The New Code


sll $10,$9,2 lw $10,a($10) addu $8,$8,$10 sw $8,sum addi $9,$9,1 sw $9,i slti $10,$9,5 bne $10,$0,loop end: beq $0,$0,end # covert "i" to word offset # load a[i] # sum = sum + a[i]; # update variable in memory # for (...; ...; i++ # update memory # for (...; i<5;

21

19/02/2009

CA&O Lecture 05 by Engr. Umbreen Sabir

22

19/02/2009

CA&O Lecture 05 by Engr. Umbreen Sabir

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,

23

19/02/2009

CA&O Lecture 05 by Engr. Umbreen Sabir

A Coding Challenge

In C

int x, y; main() { x = 0; y = 1; while (y < 100) { int t = x; x = y; y = t + y;} }

24

19/02/2009

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

You might also like