+ All Categories
Home > Documents > Natawut NupairojAssembly Language1 Sparc Architecture Overview.

Natawut NupairojAssembly Language1 Sparc Architecture Overview.

Date post: 21-Dec-2015
Category:
Upload: ada-clarke
View: 225 times
Download: 3 times
Share this document with a friend
35
Natawut Nupairoj Assembly Language 1 Sparc Architecture Overview
Transcript

Natawut Nupairoj Assembly Language 1

Sparc Architecture Overview

Natawut Nupairoj Assembly Language 2

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 3

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 4

Machine Organization DiagramCPU

MEMORY

ALU

Register

F ile

PC

IR

MAR

MDR

Natawut Nupairoj Assembly Language 5

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 6

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);

Natawut Nupairoj Assembly Language 7

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 8

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 9

Sparc Registers

• 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

Natawut Nupairoj Assembly Language 10

Sparc Registers

• 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

Natawut Nupairoj Assembly Language 11

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

Natawut Nupairoj Assembly Language 12

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 13

Operands

• Most instructions have three operands:– three registers

op 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.

Natawut Nupairoj Assembly Language 14

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.

Natawut Nupairoj Assembly Language 15

Constant in Operand

• Constant (imm) must be 13-bit signed number:-4096 <= imm < 4096

• Format of constant ???

Natawut Nupairoj Assembly Language 16

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 17

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);

}

Natawut Nupairoj Assembly Language 18

printf Function

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

var1

var2

Natawut Nupairoj Assembly Language 19

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

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

...

Natawut Nupairoj Assembly Language 20

Sparc Basic Assembly Instructions

• Load/Store Operationsmov 75, %o2 ! %o2 = 75

mov %o2, %i3 ! %i3 = %o2

clr %l4 ! %l4 = 0

• Arithmeticsadd %o1, %l2, %l3 ! %l3 = %o1 + %l2

add %o3, 19, %g4 ! %g4 = %o3 + 19

sub %i0, %g2, %o5 ! %o5 = %i0 + %g2

Natawut Nupairoj Assembly Language 21

Sparc Basic Assembly Instructions

• Multiply / Divide– To multiply: 24 * %i2mov 24, %o0 ! First operandmov %i2, %o1 ! Second operandcall .mul ! Result stored in %o0nop ! Delay slot, discussed later

! %o0 := %o0 * %o1– To divide: %o2 / %g3mov %o2, %o0 ! First operandmov %g3, %o1 ! Second operandcall .div ! Result stored in %o0nop ! Delay slot, discussed later

! %o0 = %o0 / %o1

Natawut Nupairoj Assembly Language 22

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 23

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

Natawut Nupairoj Assembly Language 24

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

Natawut Nupairoj Assembly Language 25

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 26

Creating Executable File

• Use M4 macro-processorm4 < first.m > first.s(M4 produces first.s. Notice macro expansion.)

• Compile first.sgcc -S first.s -o first(this produces an executable file “first”.)

Natawut Nupairoj Assembly Language 27

Running our First Program

• Run firstfirstx = 9 and y = 16

• Using printf to trace a program is not convenient.

Natawut Nupairoj Assembly Language 28

The gdb Debugger

• To check the result, we will use a debugger.• Run: gdb <filename>

gdb first

(gdb)

• Run your program:(gdb)rStarting program:

/usr3/faculty/natawut/Class/Assembly/first

...

Program exited with code 011.

(gdb)

Natawut Nupairoj Assembly Language 29

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 30

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.

Natawut Nupairoj Assembly Language 31

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 32

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 33

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 34

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.

Natawut Nupairoj Assembly Language 35

More Debugging Commands

• For other commands, try: help• To exit from gdb: q


Recommended