Download Sparc Architecture Overview

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Sparc Architecture Overview
Natawut Nupairoj
Assembly Language
1
Von Neumann Architecture
•
•
•
•
Designed by John von Neumann in 1949.
Machine = CPU + Memory
Program is stored in memory along with data.
CPU has Program Counter (PC) and Instruction
Register (IR)
• Use PC to keep the current location of
instruction being executed.
Natawut Nupairoj
Assembly Language
2
Von Neumann Architecture
• Control unit fetches an instruction from
memory (located by PC) and stores in IR.
• Memory = Memory Address Register (MAR) +
Memory Data Register (MDR)
• CPU puts an address in MAR and load/store
from/to MDR.
Natawut Nupairoj
Assembly Language
3
Machine Organization Diagram
CPU
MAR
A
L
U
MEMORY
Register
File
MDR
PC
IR
Natawut Nupairoj
Assembly Language
4
Instruction Execution
• Fetch-Decode-Execute cycles:
–
–
–
–
–
–
–
–
Fetch the next instruction from memory.
Change PC to point to next instruction.
Determine the type of the instruction fetched.
Find where the data being used by the instruction is
kept.
Fetch the data, if required.
Execute the instruction.
Store the results in the appropriate place.
Go to step 1 and start all over again.
Natawut Nupairoj
Assembly Language
5
Instruction Cycles
pc = 0;
do {
ir := memory[pc];
pc := pc + INSTR_SIZE;
decode(ir);
fetch(operands);
execute;
store(results);
} while(ir != HALT);
Natawut Nupairoj
{
{
{
{
{
{
Fetch the instruction. }
Move PC to next instruction. }
Decode the instruction. }
Fetch the operands. }
Execute the instruction. }
store the results. }
Assembly Language
6
Sparc Architecture Overview
• Load/Store architecture
– ALU cannot access data from memory directly.
– Data must be loaded into registers before computing.
• RISC (Reduced Instruction Set Computer)
architecture
• All instructions are one word (32 bits).
• 5-stage Pipelining CPU.
Natawut Nupairoj
Assembly Language
7
Sparc Registers
• There are 32 registers (%r0 - %r31).
• Each register is 64-bit for UltraSparc (128-bit
for UltraSparc III).
• Registers are logically divided into 4 sets:
global (%gx), in (%ix), local (%lx), and out (%ox).
• All registers are equal, can perform any
operations.
• Special register:
%g0 (%r0) - always discards writes and return zero.
Natawut Nupairoj
Assembly Language
8
Sparc Registers
• Global
%g0
%g1
%g2
%g3
%g4
%g5
%g6
%g7
%r0
%r1
%r2
%r3
%r4
%r5
%r6
%r7
• Output
readonly / return zero
Natawut Nupairoj
%o0
%o1
%o2
%o3
%o4
%o5
%o6
%o7
%r8
%r9
%r10
%r11
%r12
%r13
%r14
%r15
Assembly Language
%sp stack pointer
called sub ret addr
9
Sparc Registers
• Local
• Input
%l0
%l1
%l2
%l3
%l4
%l5
%l6
%l7
%i0
%i1
%i2
%i3
%i4
%i5
%i6
%i7
%r16
%r17
%r18
%r19
%r20
%r21
%r22
%r23
Natawut Nupairoj
%r24
%r25
%r26
%r27
%r28
%r29
%r30
%r31
Assembly Language
%fp frame pointer
sub return addr
10
Format of Instructions
• Any instruction is made up of two parts:
– Opcode (the name of the instruction)
– Operands (the values or data manipulated by the
instruction) -- can be omitted.
L1:
add
label
%i2, 0x80, %o1
opcode
Natawut Nupairoj
! Add two numbers
operands
Assembly Language
comment
11
Label
• Define the location of data or instruction.
• Start with: letter (A-Z and a-z), _, $, or .
• Followed by: letter (A-Z and a-z), number (0-9),
_, $, or .
• Must end with color (:).
Natawut Nupairoj
Assembly Language
12
Operands
• Most instructions have three operands:
– three registers
op
reg, reg, reg
add
%g1, %i2, %g2
! G2 is the destination.
– two registers and a literal constant
op
reg, imm, reg
add
%o1, 30, %o2
! O2 is the destination.
– two registers
op
reg, reg
mov
%o4, %l3
! L3 is the destination.
Natawut Nupairoj
Assembly Language
13
Operands
– a constant and a register
op
imm, reg
mov
453, %g1
– a register
op
reg
clr
%l2
– a constant
op
imm
call
myfunc
! G1 is the destination.
• Notice:
– Last one is usually the destination.
Natawut Nupairoj
Assembly Language
14
Constant in Operand
• Constant (imm) must be 13-bit signed number:
-4096 <= imm < 4096
• Format of constant ???
Natawut Nupairoj
Assembly Language
15
Comment
• Inline comment (!):
– ignore to the end of line.
! Inline comment here. Ignore to end of line.
• C-style comment (/* … */):
– ignore anything between the comment markers.
/* comment here
and it can be multiple line. */
Natawut Nupairoj
Assembly Language
16
Our First Program
• Let try some simple C program (but not
hello world !).
/* first.c -- not hello world ! as usual */
main()
{
int x, y;
x = 9;
y = (x - 2)*(x + 14)/(x + 1);
printf(“x = %d, y = %d\n”, x, y);
}
Natawut Nupairoj
Assembly Language
17
printf Function
var1
printf(“x = %d, y = %d\n”, x, y);
format
var2
• Display information formatted by the first
string.
• Format:
%d = integer
%s = string
%f = floating point
\n = newline
Natawut Nupairoj
Assembly Language
18
Our First Program
gcc -S first.c
• generate first.s
.file "first.c"
gcc2_compiled.:
.global .umul
.global .div
.section ".rodata"
.align 8
.LLC0:
.asciz "x = %d and y = %d\n"
.section ".text"
.align 4
.global main
.type main,#function
.proc 04
Natawut Nupairoj
main:
!#PROLOGUE# 0
save %sp, -120, %sp
!#PROLOGUE# 1
mov 9, %o0
st
%o0, [%fp-20]
ld
[%fp-20], %o1
add %o1, -2, %o0
ld
[%fp-20], %o2
add %o2, 14, %o1
call .umul, 0
nop
ld
[%fp-20], %o2
add %o2, 1, %o1
call .div, 0
...
Assembly Language
19
Sparc Basic Assembly
Instructions
• Load/Store Operations
mov
mov
clr
75, %o2
%o2, %i3
%l4
! %o2 = 75
! %i3 = %o2
! %l4 = 0
• Arithmetics
add
add
sub
%o1, %l2, %l3
%o3, 19, %g4
%i0, %g2, %o5
Natawut Nupairoj
! %l3 = %o1 + %l2
! %g4 = %o3 + 19
! %o5 = %i0 + %g2
Assembly Language
20
Sparc Basic Assembly
Instructions
• Multiply / Divide
– To multiply: 24 * %i2
mov 24, %o0
mov %i2, %o1
call .mul
nop
!
!
!
!
First operand
Second operand
Result stored in %o0
Delay slot, discussed later
! %o0 := %o0 * %o1
– To divide: %o2 / %g3
mov %o2, %o0
mov %g3, %o1
call .div
nop
Natawut Nupairoj
!
!
!
!
!
First operand
Second operand
Result stored in %o0
Delay slot, discussed later
%o0 = %o0 / %o1
Assembly Language
21
Our First Program (Revisited)
/* first.m */
/*
* This programs compute:
* y = (x - 2) * (x + 14) / (x + 8)
* for x = 9
*/
/* use %l0 and %l1 to store x and y */
define(x_r, l0)
define(y_r, l1)
/* define constants */
define(c1, 2)
Natawut Nupairoj
Assembly Language
22
Our First Program (Revisited)
fmt:
.asciz "x = %d and y = %d\n"
.align
4
.global main
main:
save
%sp, -64, %sp
mov
sub
add
call
nop
add
call
nop
mov
9, %x_r
%x_r, c1, %o0
%x_r, 14, %o1
.mul
!
!
!
!
%x_r, 1, %o1
.div
! %o1 = x + 1
! %o0 = %o0 / %o1
%o0, %y_r
! store result in y
Natawut Nupairoj
x =
%o0
%o1
%o0
9
= x - 2
= x + 14
= %o0 * %o1
Assembly Language
23
Our First Program (Revisited)
set
mov
mov
call
nop
fmt, %o0
%x_r, %o1
%y_r, %o2
printf
!
!
!
!
mov
ta
1, %g1
0
! prepare to exit
! normal exit
Natawut Nupairoj
first argument for printf
second argument for printf
third argument for printf
print them out
Assembly Language
24
Directives
• Information for the assembler.
• .global - tell the assembler the name of this
function.
• .asciz - define a string.
• .align - align a location counter on a boundary.
Natawut Nupairoj
Assembly Language
25
Creating Executable File
• Use M4 macro-processor
m4 < first.m > first.s
(M4 produces first.s. Notice macro expansion.)
• Compile first.s
gcc -S first.s -o first
(this produces an executable file “first”.)
Natawut Nupairoj
Assembly Language
26
Running our First Program
• Run first
first
x = 9 and y = 16
• Using printf to trace a program is not
convenient.
Natawut Nupairoj
Assembly Language
27
The gdb Debugger
• To check the result, we will use a debugger.
• Run: gdb <filename>
gdb first
(gdb)
• Run your program:
(gdb)r
Starting program:
/usr3/faculty/natawut/Class/Assembly/first
...
Program exited with code 011.
(gdb)
Natawut Nupairoj
Assembly Language
28
Breakpoint
• Set a breakpoint:
(gdb) b main
Breakpoint 1 at 0x105a4
(gdb) r
Starting program:
/usr3/faculty/natawut/Class/Assembly/first
...
Breakpoint 1, 0x105a4 in main ()
(gdb)
Natawut Nupairoj
Assembly Language
29
Print an Instruction
(gdb) x/i $pc
0x105a4 <main+4>:
(gdb)
0x105a8 <main+8>:
(gdb)
mov
9, %l0
sub
%l0, 2, %o0
• We can examine memory by typing “x”.
• Tell gdb to interpret the current memory as an
instruction.
• Use current location pointed by %pc.
• Repeat last command by hitting enter key.
Natawut Nupairoj
Assembly Language
30
Print the Entire Program
(gdb) disassemble
Dump of assembler code for function main:
0x105a0 <main>: save %sp, -64, %sp
0x105a4 <main+4>:
mov 9, %l0
0x105a8 <main+8>:
sub %l0, 2, %o0
0x105ac <main+12>:
add %l0, 0xe, %o1
0x105b0 <main+16>:
call 0x2069c <.mul>
0x105b4 <main+20>:
nop
...
0x105d4 <main+52>:
add %o7, %l7, %l7
End of assembler dump.
(gdb)
Natawut Nupairoj
Assembly Language
31
More Debugging Commands
• Advance breakpoint:
(gdb) b *& main+16
Breakpoint 3 at 0x105cc
(gdb) c
Continuing.
Breakpoint 3, 0x105cc in main ()
(gdb)
• We use “c” to continue execution after stopping
at a breakpoint.
Natawut Nupairoj
Assembly Language
32
More Debugging Commands
• Print out the contents of a register:
(gdb) p $l0
$1 = 9
(gdb)
• Automatically print out contents:
(gdb) display/i $pc
1: x/i $pc 0x105a4 <main+4>:
mov 9, %l0
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program:
/usr3/faculty/natawut/Class/Assembly/first
Natawut Nupairoj
Assembly Language
33
More Debugging Commands
Breakpoint 2, 0x105a4 in main ()
1: x/i $pc 0x105a4 <main+4>:
mov
(gdb) ni
0x105a8 in main ()
1: x/i $pc 0x105a8 <main+8>:
sub
(gdb)
9, %l0
%l0, 2, %o0
• We use “r” to restart execution from the
beginning and “ni” to execute the next
instruction.
Natawut Nupairoj
Assembly Language
34
More Debugging Commands
• For other commands, try: help
• To exit from gdb: q
Natawut Nupairoj
Assembly Language
35
Related documents