Está en la página 1de 14

Computer Architecture

Lecture 3
Computer Organization and Assembly
Programming Basics
100
10
= 0110 0100
2
(8-bit) = 0x64 =
0000 0000 0110 0100
2
(16-bit) = 0x0064 =
0000 0000 0000 0000 0000 0000 0110 0100
2
(32-bit) =
0x00000064
-100
10
= 1001 1100
2
(8-bit) =0x9C
= 1111 1111 1001 1100
2
(16-bit) = 0xFF9C
= 1111 1111 1111 1111 1111 1111 1001 1100
2
(32-bit) =
0xFFFFFF9C
When converting always ask In how many bits,
especially tive numbers?
Hex representations are ONLY for human reading
as they are compact
Review: 2c Numbers
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 10
i DD ?
j DD ?
Any program has many distinct areas. For
example, space for static variables (.data),
space for program code (.text), space for
dynamic variables/heap/stack etc
All programs/applications must have a label
main. (.globl main, check spellings)
In MIPS, .text space always starts from
[0x00400024] //your first instruction
In MIPS, .data space always starts from
[0x10010000] //your first variable
Some Basic Concepts on Assembly Language
Int count = 10; // C code
.data // Assembly Code
count: .word 10 // count is int type
Count variable created in memory, but where, assembler
knows that 32-bit address
How to access count variable?
We need two register, one for address other
for data
.text
la $t1, count // assembler will automatically
// load address of count in $t1
lw$t2, 0($t1) // $t2 is now 10
Practical Side, Loading a Variable
Byte size variables
MIPS and Intel
char sum = 1, total= 5;
char uni[10] =university;
Sum: .byte 1
total : .byte 5
Uni: .byte u,n, i etc;
sum db 1
total db 5
uni db u,n, i etc;
Strings MIPS and Intel
string str (hello world);
Str: .asciiz hello world
str db hello
world
Arrays MIPS and Intel
int age[100];
Age: .word 0:100 ; 100 words initialized to zero
Age dw100 dup(0); 100 words initialized to zero
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 count
addi R2, R2, 1 # Increment
sw R2, 0(R1) # Save count, if need be
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
Program Counter is user invisible register in MIPS
that points to current instruction being executed.
It is 32-bit register always wanders in .text area
only
First value of PC is first instruction of your
program at 0x00400024, next at 0x0040028,
next at 0x0040002C, next at 0x0040030, and so
on
Each time program counter is incremented by 4
after each clock cycle and instructions are
executed one by one.
But some times we do not want to go to the next
instruction??? The concept of branching.
Program Counter and Branches
Control
count = 0;
while (count < 21)
{
age[count] = age[count+1];
count = count+1;
}
addi R2, R0, 0 ; R2 = count = 0
lable1:
sll R6, R2, 2 ; R6 has address of
; count
th
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<21
bnez R8, label1
For Statement
Total = 0;
For (i= 0; i<count; i ++)
{ total = age[i] + total;}
la R1, count #R1 has address of count
lw R2, 0(R1) # R2 has the value of count
addi R5, R0, 0 # i = 0 ; R5 is i
addi 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 until
sll R6, R5, 2 # R6 has address of i
th
element of age
add R6, R6, R4 # R4 = address of age
lw R20, 0(R6) #R20 = age[i]
add R15, R15, R20 # total = total + age[i]
sw R15, 0(R22) # R22 is address of total //can be after Labl
addi R5,R5,1 # i = i+1;
b Xy: # Unconditional branch
Labl:
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, else
addi R5, R0, 5
b exit
else: addi R5, R0, 20
exit:

También podría gustarte