You are on page 1of 10

Computer Architecture

Lecture

Assembly Programming Basics

Intro to Assembly Language


MIPS and Intel

Variables and Constants int count = 10 I, j, k; count .word 10 i .word j .word

Count DD
i j DD DD

10
? ?

Byte size variables


char sum = 1, total= 5; char uni[10] =university;
sum total uni .byte 1 .byte 5 .byte u,n, i etc; sum total uni db 1 db 5 db u,n, i etc;

Strings
string str (hello world); str .asciiz hello world

str db hello world

Arrays
int age[100];
age .word 0:100 ; 100 words initialized to zero age dw 100 dup(0); 100 words initialized to zero

Assignment
count = count +1;
la lw addi sw R1, count R2, 0(R1) R2, R2, 1 R2, 0(R1) # (load address works in spim) #R1 has address of count # R2 has the value of count # Increment # Save count, if need be

Another Example
i = age[20] +age[0] + count;
la lw lw add add R3, R4, R5, R4, R4, age 80[R3] 0(R3) R4, R5 R4, R2 #R3 has base address of age; #20 * 4 = 80, R4 = age[20] #R5 = age[0] # #R2 had value of count last slide

Control
count = 0; while (count < 21) { age[count] = age[count+1]; count = count+1; }

addi lable1: sll add

R2, R0, 0

; R2 = count = 0

R6, R2, 2
R6, R6, R4

; R6 has address of ; countth element


:R2= count, R4 = age ;R6 = address of age [count] ;R7 = age[count+1] ; age[count] = R7 ; Count = count +1; ; ;R8 = 1 if count<21

lw sw
addi slti bnez

R7, 4(R6) R7, 0(r6)


R2, R2, 1 R8, R2, 21 R8, label1

For Statement
Total = 0; For (i= 0; i<count; i ++) { total = age[i] + total;}
la lw addi addi Xy: slt be sll add lw add sw addi b Labl: R1, age R2, 0(R1) R5, R0, 0 R15, R0, 0 R11,R5,R10 R11, R0, labl R6, R5, 2 R6, R6, R4 R20, 0(R6) R15, R15, R20 R15, 0(R22) R5,R5,1 Xy: #R1 has address of count # R2 has the value of count # i = 0 ; R5 is i # total = 0 ; R15 is total # check R5 is R5 < R10 (R10 is count) # loop until # R6 has address of ith element of age # R4 = address of age #R20 = age[i] # total = total + age[i] # R22 is address of total # i = i+1; # Unconditional branch

IF then
If (count == max) I = 5; else I = 20; #assume R4 has bne addi b else: addi exit: the value of count, R7 has value of MAX and I is R5 R4, R7, else R5, R0, 5 exit R5, R0, 20

You might also like