Post on 14-Dec-2015
Cosc 2150:Computer Organization
ARC assembly codeSimplified SPARC language
ARC Hardware
• There are 32 32-bit registers. %r0 through %r31.
• Reserved registers:—%r0 is always 0.
– Storing anything in %r0 will be lost.
• %r14 is a stack pointer register— also called %sp
• %r15 is link register (used for subroutine calls)
The ARCTools Simulator Window
• There are two addition registers—%pc, a 32-bit program counter register
—%psr, which is a processor status register Where arithmetic/logic flags are set.
—They are N,Z,V,C, – where N is a negative flag
– Z is the zero flag
– V is the overflow flag
– C is the carry flag.
• The lowest 211 = 2048 addresses of the memory map are reserved for use by the operating system.
A Basic ARC assembly program
.begin
.org 2048 !Lowest space in memory a program can go
main: !special label, for start of your code
!Assembly instructions
halt !end of assembly code
var: value ! variable: value of the variable.
.end
Arithmetic instructions
• All arithmetic and logic instructions have 3 opcode: instruction op1, op2, op3.
– Only op2 can be immediate load/ number otherwise, they will refer to a register.
– Any instructions that end in a cc, set condition codes (NZVC)
• addcc %r0, 1, %r1—(%r1 = 0 + 1) would set N=0, Z=0, V=0, and C=0
• subcc %r0, 1, %r1— (%r1 = 0 – 1) would set N=1, Z=0, V=1, and C=0
• Assuming %r1 = 1, subcc %r1, 1, %r1— (%r1 = %r1 –1), would set N=0, Z=1, V=0, and C=0
Data transfer instructions
• All have 2 opcode instructions.• load:
—ld [x], %r1 !load the value of x into %r1
—ld %r1+x, %r2 !load the value found by %r1 +x (offset) into %r2
• store:—st %r1, [x] !store the value of %r1 into x
—st %r2, %r1+x ! store the value of %r2 into the address %r1 +x
Branch instructions• All but one branch instruction have a single opcode in the
following format—branch label ! branch conditionally or unconditionally to the label
• example:ba done !branch to the label done! assembly code
done: halt ! stop the program
• be, bcs, bcc, bneg, bvs, bvc, bne, bpos all use the NZVC condition flags to as the condition whether to branch or not.
• jmpl and call are used for subroutines, which will be covered later.
The ARCTools Edit Window
The ARCTools Edit Window
• • The Edit window with an asm file and the file dialog
Assembly• The arc4 program after assembly, showing arc4.lst, the
listing file.
Binary File• The arc4 bin file, displayed after pressing the Show Binary
File button.
Simulation• The ARCTools simulator window after pressing
Bin -> Sim.
Simulator note.
• You need a blank line at the end of the program or the program will not compile with normally a very strange error that makes no sense.
ARC example code (1)
Example 1
main () {
int a=15, b=5,c;
c = a+ b;
}
.begin
.org 2048
main: ! c = a +b
ld [a], %r1
ld [b], %r2
addcc %r1, %r2, %r3
st %r3, [c]
halt
a: 15
b: 5
c: 0
.end
ARC Example code (2)
Example 2main() { int a =2, c=0; if (a == 2) { c =a*4; } else { c = a/2; }}
.begin
.org 2048main: ld [a], %r1 !if (a==2) subcc %r1, 2, %r0 bne else ! true c= a*4 sll %r1, 2, %r1 ba done ! false c = a/2else: srl %r1, 1, %r1done: st %r1, [c] halta: 2c: 0.end
ARC code example (3)
Example 3
main () {
int a=15, b=5, c;
if (a>=b)
c = a - b;
else
c a + b;
}
.begin
.org 2048main: ld [a], %r1
ld [b], %r2 !if (a>=b)
subcc %r1, %r2, %r3bneg falsest %r3, [c]ba done
false: add %r1, %r2, %r3st %r3, [c]
done: halta: 15b: 5c: 0.end
ARC code example (4)
Example 4
main () {
int x = 0;
while (x <5) {
x = x +1;
}
.begin
.org 2048
main: ld [x], %r1
!while (x<5)
top: subcc %r1, 5, %r0
bpos done
!x = x+1
add %r1, 1, %r1
st %r1, [x]
ba top
done: halt
x: 0
.end
ARC code example (4 modified)
• Example 4
main () {
int x = 0;
while (x <=5) {
x = x +1;
}
.begin
.org 2048main: ld [x], %r1top: subcc %r1, 5, %r2
subcc %r2, 1, %r0bpos doneadd %r1, 1, %r1st %r1, [x]ba top
done: haltx: 0.end
Helpful things to know
• What is the logic to figure out the following
• If (a>=b) or (a>=1) or any constant• If (a>b) or (a> 1)• If (a < b) or (1 < b)• If (a <= b) or (1 <= b)• If (a==1) or (a==b)• Complex if statements
—If ( (a >10) && (b < 10) )
ARC code example (5)
• Example 5
int a=2, b=2, i=1;
while (i < 5) {
a += b;
++i;
}
}
• Give it a try
ARC code example (5 cont.).begin.org 2048main: ld [i], %r1top: subcc %r1, 5, %r0
bpos doneld [a], %r2ld [b], %r3add %r2, %r3, %r2st %r2, [a]add %r1, 1, %r1st %r1, [i]ba top
done:halt
a: 2
b: 2
i: 1
.end
QA&