51
SPARC Architecture & Assembly Language Produced by Sun Microsystems

SPARC Architecture & Assembly Language Produced by Sun Microsystems

Embed Size (px)

Citation preview

Page 1: SPARC Architecture & Assembly Language Produced by Sun Microsystems

SPARC Architecture & Assembly Language

Produced by

Sun Microsystems

Page 2: SPARC Architecture & Assembly Language Produced by Sun Microsystems

A Load/Store Architecture All arithmetic/logic operations use

operand(s) found in the register(s) Result is stored in a register Load and store instructions are

provided to move data from/to memory

All registers hold 32 bits of data

Page 3: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Registers Registers eliminate access to

system bus and memory Registers provide rapid, direct

access to operands Each function of the program has

32 registers available to it at any on time

Four sets of eight registers each: global, in, local, and out.

Page 4: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Global Registers %g0 - %g7 Used for global data, data that has

meaning for the entire program Accessible to any function %g0 always 0 Avoid using %g1. It is used by the

system

Page 5: SPARC Architecture & Assembly Language Produced by Sun Microsystems

In Registers %i0 - %i7 Used to receive values of

parameters to a function Described in chapter 7 Avoid using %i6 and %i7

Page 6: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Out Registers %o0 - %o7 Used to pass values to functions Used to return values from a

function Described in chapter 7 Avoid using %o6 and %o7

Page 7: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Local Registers %l0 - %l7 Used to store a function’s local

variables We use the registers in the early

chapters

Page 8: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Assembly Language Two pass assembler First pass determines the address

of each instruction A label (a name followed by :) is

given an address at this time Second pass uses these addresses

in generating code

Page 9: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Instruction Format Case sensitive Label Operation Operands, separated by comma(s) Comment Start: add %o0, %o1, %l0 !%l0 = %o0 + %o1

Page 10: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Labels Follow usual rules for variable

names Must end in a colon : Its value is the address of the

instruction for which it is a label Variables, function start, target of

branch instructions

Page 11: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Comments C-like comments

/* Lines of text */

One-line comments ! Line of text

Page 12: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Macro Definitions Equivalent to #define in C Processed by the m4 macro

preprocessor before compilation Uses a literal text string substitution define( text1, text2) Can be very complex, BUT keep it

simple

Page 13: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Examples define(a2, 1) ^ no blanks Preprocessor substitutes 1 for

every occurrence of a2

Page 14: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Examples define(a2, 1) ! #define a2 1

define(a1, 7)define(a0, 11)define(x_r, %l0) !that's an ell

zerodefine(y_r, %l1)

Page 15: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Pseudo-ops Not really operations Provided by assembler See page 424, Appendix D (1st

edition) appendix E in 2nd edition Used primarily to define storage

for static variables Used to mark beginning of function

Page 16: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Example Marking a function

.global mainmain: save %sp, -64, %sp

Page 17: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Program Structure Introductory Comments Constants and defines Storage for static, global variables Function name definition

using .global Function body Function return

Page 18: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Pipeline Most computers today use pipeline

techniques Provides faster execution Execution cycle more complicated Need to undo because of branches

in code See Figure 2.1 and 2.2

Page 19: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Sparc Consequence Every branch or call instruction

must be followed with an instruction Called the delay slot

Fill with instruction, maybe nop Branch instructions – see Appendix

C.7 call or b_ _ _ instructions

Page 20: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Example 2.6 Our goal is to write an assembly

language program to compute the value of

y = (x - 1) * (x - 7) / (x - 11) for x = 9

No input / output is used

Page 21: SPARC Architecture & Assembly Language Produced by Sun Microsystems

C Code for the problem#define a2 1#define a1 7#define a0 11

void main(){ register int x; register int y; y = (x - a2) * (x - a1) / (x - a0); exit(0);}

Page 22: SPARC Architecture & Assembly Language Produced by Sun Microsystems

ex02.6.m (1) !**************************************************!

File: ex02.6.s! Dir: cis235/suns! Date: December 1, 1998! Author: HGG ! Computer: KUNET suns! Assembler: as under the gcc compiler ! Compile: sa ex02.6! Execute: a.out !! Purpose: to compute the expression! y = (x - 1) * (x - 7) / (x - 11)

! For the value x = 9!**************************************************

Page 23: SPARC Architecture & Assembly Language Produced by Sun Microsystems

ex02.6.m (2) !***** const section

define(a2, 1)define(a1, 7)define(a0, 11)

!***** variable section ! C code! register int x_r! register int y_r

define(x_r, %l0) ! that's an ell zerodefine(y_r, %l1)

Page 24: SPARC Architecture & Assembly Language Produced by Sun Microsystems

ex02.6.m (3) ! void main()

.global mainmain: save %sp, -64, %sp

Page 25: SPARC Architecture & Assembly Language Produced by Sun Microsystems

ex02.6.m (4) ! y = (x – a2)*(x – a1) / (x – a0) mov 9, x_r ! x_r = 9

sub x_r, a2, %o0 ! o0 = x_r - a2sub x_r, a1, %o1 ! o1 = x_r - a1call .mul ! o0 = o0 * o1nopsub x_r, a0, %o1 ! o1 = x_r - a0call .div ! o0 = o0 / o1nopmov %o0, y_r ! y_r = o0

Page 26: SPARC Architecture & Assembly Language Produced by Sun Microsystems

ex02.6.m (5) ! exit(0)mov 0, %o0call exitnop

! mov 1, %g1! ta 0

Page 27: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Filling Delay Slots Delayed control transfer (branch

instruction) The instruction following the branch

instruction is always executed before the branch is taken

This instruction is said to be in the delay slot

We would like to fill the delay slot with a meaningful instruction other than a nop

Page 28: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Ex02.6.m revisited (1) ! y = (x – a2)*(x – a1) / (x – a0) mov 9, x_r ! x_r = 9

sub x_r, a2, %o0 ! o0 = x_r - a2subsub x_r, a1, %o1 ! o1 = x_r - a1x_r, a1, %o1 ! o1 = x_r - a1

call .mul ! o0 = o0 * o1sub x_r, a1, %o1 ! o1 = x_r - a1

subsub x_r, a0, %o1 ! o1 = x_r - a0x_r, a0, %o1 ! o1 = x_r - a0 call .div ! o0 = o0 / o1

sub x_r, a0, %o1 ! o1 = x_r - a0

mov %o0, y_r ! y_r = o0

Page 29: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Ex02.6.m revisited (2) ! call exit(0)

call exitmov 0, %o0

Page 30: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Summary We can now add, subtract,

multiply, and divide We can define constants We can define mnemonics to allow

us to use more meaningful names We know how to exit to the OS

Page 31: SPARC Architecture & Assembly Language Produced by Sun Microsystems

The Debugger gdb (Cf. 2.7)

Learning how to use the debugger is useful for C/C++ program development as well as for the assembler

File must be compiled with –g option Called by the command

gdb a.outorgdb executable_file

Page 32: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Program Address Space

Code SectionCode

static variablesOS memory

Heap Sectiondynamic variables

Stack Sectionautomatic variables

Page 33: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Code section Contains storage for

Code Operating System information Static variables – global and local

Page 34: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Stack section Contains automatic variables of

the functions Contains frame information for

each call of a function

Page 35: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Heap section Contains dynamic variables – those

objects created by the new function in C++ or the malloc function in C and destroyed by the delete function.

Page 36: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Defining Static Global Variables

Static global variables in C/C++ are those variables defined outside of a function

Contrast to automatic variables They are created and compiled

Page 37: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Integer Variables Int comes in three flavors

short .byte int .half long .word

Examples in C short sum = 0; int vecSize = 5; long i = -1;

Page 38: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Assembler Equivalent

.align 4sum: .byte 0 .align 2!move to next spot that can hold half word

vecSize: .half 5 .align 4 !move to next spot that can hold word

i: .word -1 ! align causes location counter to be divisible by its

argument

Page 39: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Strings

A C string equivalent A null terminated string of characters

enclosed in “ “ Can contain escape characters, e.g. \

n, \t, etc. string prompt = “Enter an integer: “; string message = “Too much data\

n”;

Page 40: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Assembler equivalent

.align 4prompt: .asciz “Enter an

integer: “.align 4

message: .asciz “Too much data\n”

Page 41: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Loading Variables Need to have the address of the

variable in a register ld [src_reg], dest_reg

src_reg contains the address of the variable

dest_reg will contain the value of the variable at that address

Page 42: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Getting Addresses into a Register

Need two instructions:sethi %hi(name), dest_regor dest_reg, %lo(name),

dest_reg%hi: higher 22 bits of 32 bit register%hi: higher 22 bits of 32 bit register

%lo: low 10 bits%lo: low 10 bits

The assembler provides a shortcutset name, dest_reg

Page 43: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Example Consider again the C code and its equivalent

assembler

! int sum = 0; sum:.long0

sethi %hi(sum), %o1or %o1, %lo(sum), %o1

or set sum, %o1

Page 44: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Compute sum + x; Code to compute sum + x set sum, %o1 ! o1 =

addr(sum) ld [%o1], %o1 ! o1 = sum set x, %o2 ! o2 = addr(x) ld [%o2], %o2 ! o2 = x add %o1, %o2, %o2 ! o2 = sum + x

Page 45: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Using printf printf is a formatted print statement See a C reference manual for details printf(address of message); printf(format string address, list of

variables); The parameters go in the “o” registers from

left to right starting with register %o0.

Page 46: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Printing a Message (1)

! string message = “Hello, world\n”;message: .asciz “Hello, world\n”…! printf(message);set message, %o0call printfnop

Page 47: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Printing a Message (2)

sethi %hi(message), %o0call printfor %o0, %lo(message),

%o0

delay slot

Page 48: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Printf(format, list of values) Parameters go in the o registers,

left to right starting at o0. You cannot use registers o6 and o7

The address of the format in o0 The values of the variables in

successive o registers

Printing Values

Page 49: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Examplefmto: .asciz “x = %d, y = %d, z = %d\n”

! printf(fmto, x, y, z);set fmto, %o0set x, %o1ld [%o1], %o1set y, %o2ld [%o2], %o2set z, %o3ld [%o3], %o3call printfnop

Page 50: SPARC Architecture & Assembly Language Produced by Sun Microsystems

Using scanf() scanf is a formatted read statement See a C reference manual for details scanf(format string address, list of

address of variables); The parameters go in the “o” registers

from left to right starting with register %o0.

Page 51: SPARC Architecture & Assembly Language Produced by Sun Microsystems

scanf () example

x: .word 0fmti: .asciz “%d “…!scanf(fmti, &x);set fmti, %o0set x, %o1call scanfnop