530 likes | 751 Views
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
E N D
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
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.
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
In Registers • %i0 - %i7 • Used to receive values of parameters to a function • Described in chapter 7 • Avoid using %i6 and %i7
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
Local Registers • %l0 - %l7 • Used to store a function’s local variables • We use the registers in the early chapters
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
Instruction Format • Case sensitive • Label • Operation • Operands, separated by comma(s) • Comment • Start: add %o0, %o1, %l0 !%l0 = %o0 + %o1
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
Comments • C-like comments • /* • Lines of text • */ • One-line comments • ! Line of text
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
Examples • define(a2, 1) • ^ no blanks • Preprocessor substitutes 1 for every occurrence of a2
Examples • define(a2, 1) ! #define a2 1 define(a1, 7) define(a0, 11) define(x_r, %l0) !that's an ell zero define(y_r, %l1)
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
Example • Marking a function .global main main: save %sp, -64, %sp
Program Structure • Introductory Comments • Constants and defines • Storage for static, global variables • Function name definition using .global • Function body • Function return
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
Sparc Consequence • Every branchor 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
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
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); }
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!**************************************************
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 zero define(y_r, %l1)
ex02.6.m (3) • ! void main() .global mainmain: save %sp, -64, %sp
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 - a2 sub x_r, a1, %o1 ! o1 = x_r - a1 call .mul ! o0 = o0 * o1nop sub x_r, a0, %o1 ! o1 = x_r - a0 call .div ! o0 = o0 / o1nop mov %o0, y_r ! y_r = o0
ex02.6.m (5) • ! exit(0)mov 0, %o0call exitnop! mov 1, %g1! ta 0
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
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 - a2 sub x_r, a1, %o1 ! o1 = x_r - a1 call .mul ! o0 = o0 * o1sub x_r, a1, %o1 ! o1 = x_r - a1 sub x_r, a0, %o1 ! o1 = x_r - a0 call .div ! o0 = o0 / o1sub x_r, a0, %o1 ! o1 = x_r - a0 mov %o0, y_r ! y_r = o0
Ex02.6.m revisited (2) • ! call exit(0) call exitmov 0, %o0
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
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.out or gdb executable_file
Program Address Space Code Section Code static variables OS memory Heap Section dynamic variables Stack Section automatic variables
Code section • Contains storage for • Code • Operating System information • Static variables – global and local
Stack section • Contains automatic variables of the functions • Contains frame information for each call of a function
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.
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
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;
Assembler Equivalent .align 4 sum: .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
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”;
Assembler equivalent .align 4 prompt: .asciz “Enter an integer: “ .align 4 message: .asciz “Too much data\n”
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
Getting Addresses into a Register • Need two instructions: sethi %hi(name), dest_reg or dest_reg, %lo(name), dest_reg %hi: higher 22 bits of 32 bit register %lo: low 10 bits • The assembler provides a shortcut set name, dest_reg
Example • Consider again the C code and its equivalent assembler ! int sum = 0; sum: .long 0 sethi %hi(sum), %o1 or %o1, %lo(sum), %o1 or set sum, %o1
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
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.
Printing a Message (1) ! string message = “Hello, world\n”; message: .asciz “Hello, world\n” … ! printf(message); set message, %o0 call printf nop
Printing a Message (2) sethi %hi(message), %o0 call printf or %o0, %lo(message), %o0 delay slot
Printing Values • 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
Example fmto: .asciz “x = %d, y = %d, z = %d\n” ! printf(fmto, x, y, z); set fmto, %o0 set x, %o1 ld [%o1], %o1 set y, %o2 ld [%o2], %o2 set z, %o3 ld [%o3], %o3 call printf nop
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.