360 likes | 403 Views
Sparc Architecture Overview. 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.
E N D
Sparc Architecture Overview Assembly Language
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. Assembly Language
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. Assembly Language
Machine Organization Diagram Assembly Language
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. Assembly Language
Instruction Cycles pc = 0; do { ir := memory[pc]; { Fetch the instruction. } pc := pc + INSTR_SIZE; { Move PC to next instruction. } decode(ir); { Decode the instruction. } fetch(operands); { Fetch the operands. } execute; { Execute the instruction. } store(results); { store the results. } } while(ir != HALT); Assembly Language
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. Assembly Language
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. Assembly Language
Global %g0 %r0 readonly / return zero %g1 %r1 %g2 %r2 %g3 %r3 %g4 %r4 %g5 %r5 %g6 %r6 %g7 %r7 Output %o0 %r8 %o1 %r9 %o2 %r10 %o3 %r11 %o4 %r12 %o5 %r13 %o6 %r14 %sp stack pointer %o7 %r15 called sub ret addr Sparc Registers Assembly Language
Local %l0 %r16 %l1 %r17 %l2 %r18 %l3 %r19 %l4 %r20 %l5 %r21 %l6 %r22 %l7 %r23 Input %i0 %r24 %i1 %r25 %i2 %r26 %i3 %r27 %i4 %r28 %i5 %r29 %i6 %r30 %fp frame pointer %i7 %r31 sub return addr Sparc Registers Assembly Language
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 %i2, 0x80, %o1 ! Add two numbers label opcode operands comment Assembly Language
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 (:). Assembly Language
Operands • Most instructions have three operands: • three registersop reg, reg, regadd %g1, %i2, %g2 ! G2 is the destination. • two registers and a literal constantop reg, imm, regadd %o1, 30, %o2 ! O2 is the destination. • two registersop reg, regmov %o4, %l3 ! L3 is the destination. Assembly Language
Operands • a constant and a registerop imm, regmov 453, %g1 ! G1 is the destination. • a registerop regclr %l2 • a constantop immcall myfunc • Notice: • Last one is usually the destination. Assembly Language
Constant in Operand • Constant (imm) must be 13-bit signed number: -4096 <= imm < 4096 • Format of constant ??? Assembly Language
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. */ Assembly Language
Our First Program • Let try some simple C program (but nothello 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); } Assembly Language
printf Function var1 printf(“x = %d, y = %d\n”, x, y); • Display information formatted by the first string. • Format: %d = integer %s = string %f = floating point \n = newline format var2 Assembly Language
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 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 ... Our First Program Assembly Language
Sparc Basic Assembly Instructions • Load/Store Operations mov 75, %o2 ! %o2 = 75 mov %o2, %i3 ! %i3 = %o2 clr %l4 ! %l4 = 0 • Arithmetics add %o1, %l2, %l3 ! %l3 = %o1 + %l2 add %o3, 19, %g4 ! %g4 = %o3 + 19 sub %i0, %g2, %o5 ! %o5 = %i0 + %g2 Assembly Language
Sparc Basic Assembly Instructions • Multiply / Divide • To multiply: 24 * %i2 mov 24, %o0 ! First operand mov %i2, %o1 ! Second operand call .mul ! Result stored in %o0 nop ! Delay slot, discussed later ! %o0 := %o0 * %o1 • To divide: %o2 / %g3 mov %o2, %o0 ! First operand mov %g3, %o1 ! Second operand call .div ! Result stored in %o0 nop ! Delay slot, discussed later ! %o0 = %o0 / %o1 Assembly Language
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) Assembly Language
Our First Program (Revisited) fmt: .asciz "x = %d and y = %d\n" .align 4 .global main main: save %sp, -64, %sp mov 9, %x_r ! x = 9 sub %x_r, c1, %o0 ! %o0 = x - 2 add %x_r, 14, %o1 ! %o1 = x + 14 call .mul ! %o0 = %o0 * %o1 nop add %x_r, 1, %o1 ! %o1 = x + 1 call .div ! %o0 = %o0 / %o1 nop mov %o0, %y_r ! store result in y Assembly Language
Our First Program (Revisited) set fmt, %o0 ! first argument for printf mov %x_r, %o1 ! second argument for printf mov %y_r, %o2 ! third argument for printf call printf ! print them out nop mov 1, %g1 ! prepare to exit ta 0 ! normal exit Assembly Language
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. Assembly Language
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”.) Assembly Language
Running our First Program • Run first first x = 9 and y = 16 • Using printf to trace a program is not convenient. Assembly Language
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) Assembly Language
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) Assembly Language
Print an Instruction (gdb) x/i $pc 0x105a4 <main+4>: mov 9, %l0 (gdb) 0x105a8 <main+8>: sub %l0, 2, %o0 (gdb) • 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. Assembly Language
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) Assembly Language
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. Assembly Language
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 Assembly Language
More Debugging Commands Breakpoint 2, 0x105a4 in main () 1: x/i $pc 0x105a4 <main+4>: mov 9, %l0 (gdb) ni 0x105a8 in main () 1: x/i $pc 0x105a8 <main+8>: sub %l0, 2, %o0 (gdb) • We use “r” to restart execution from the beginning and “ni” to execute the next instruction. Assembly Language
More Debugging Commands • For other commands, try: help • To exit from gdb: q Assembly Language