CA Assembly Language Basics.ppt

10
Computer Architecture Lecture Assembly Programming Basics

Transcript of CA Assembly Language Basics.ppt

Page 1: CA Assembly Language Basics.ppt

Computer Architecture

Lecture

Assembly Programming Basics

Page 2: CA Assembly Language Basics.ppt

Intro to Assembly LanguageMIPS and Intel

Variables and Constants

int count = 10 I, j, k;

count .word 10i .word j .word

Count DD10

i DD ?

j DD ?

Page 3: CA Assembly Language Basics.ppt

Byte size variables

char sum = 1, total= 5;char uni[10] =“university”;

sum .byte 1total .byte 5uni .byte “u”,”n”, “i” etc;

sum db 1total db 5uni db “u”,”n”, “i” etc;

Page 4: CA Assembly Language Basics.ppt

Strings

string str (“hello world”);

str .asciiz “hello world”

str db “hello world”

Page 5: CA Assembly Language Basics.ppt

Arrays

int age[100];

age .word 0:100 ; 100 words initialized to zero

age dw 100 dup(0); 100 words initialized to zero

Page 6: CA Assembly Language Basics.ppt

Assignment

count = count +1;

la R1, count # (load address works in spim) #R1 has address of count

lw R2, 0(R1) # R2 has the value of countaddi R2, R2, 1 # Incrementsw R2, 0(R1) # Save count, if need be

Page 7: CA Assembly Language Basics.ppt

Another Example

i = age[20] +age[0] + count;la R3, age #R3 has base address of

age;lw R4, 80[R3] #20 * 4 = 80, R4 = age[20]lw R5, 0(R3) #R5 = age[0]add R4, R4, R5 #add R4, R4, R2 #R2 had value of count

last slide

Page 8: CA Assembly Language Basics.ppt

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

addi R2, R0, 0 ; R2 = count = 0lable1:

sll R6, R2, 2 ; R6 has address of ; countth element

add R6, R6, R4 :R2= count, R4 = age;R6 = address of age [count]

lw R7, 4(R6) ;R7 = age[count+1]sw R7, 0(r6) ; age[count] = R7

addi R2, R2, 1 ; Count = count +1;slti R8, R2, 21 ;

;R8 = 1 if count<21bnez R8, label1

Page 9: CA Assembly Language Basics.ppt

For Statement

Total = 0;For (i= 0; i<count; i ++){ total = age[i] + total;}

la R1, age #R1 has address of countlw R2, 0(R1) # R2 has the value of countaddi R5, R0, 0 # i = 0 ; R5 is iaddi R15, R0, 0 # total = 0 ; R15 is total

Xy: slt R11,R5,R10 # check R5 is R5 < R10 (R10 is count)be R11, R0, labl # loop untilsll R6, R5, 2 # R6 has address of ith element of ageadd R6, R6, R4 # R4 = address of agelw R20, 0(R6) #R20 = age[i]add R15, R15, R20 # total = total + age[i]sw R15, 0(R22) # R22 is address of totaladdi R5,R5,1 # i = i+1; b Xy: # Unconditional branch

Labl:

Page 10: CA Assembly Language Basics.ppt

IF … then

If (count == max) I = 5; else I = 20;

#assume R4 has the value of count, R7 has value of MAX and I is R5

bne R4, R7, elseaddi R5, R0, 5b exit

else: addi R5, R0, 20exit: