Presentation is loading. Please wait.

Presentation is loading. Please wait.

SPARC Architecture & Assembly Language

Similar presentations

Presentation on theme: "SPARC Architecture & Assembly Language"— Presentation transcript:

1 SPARC Architecture & Assembly Language
Produced by Sun Microsystems

2 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

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

4 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

5 In Registers %i0 - %i7 Used to receive values of parameters to a function Described in chapter 7 Avoid using %i6 and %i7

6 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

7 Local Registers %l0 - %l7 Used to store a function’s local variables
We use the registers in the early chapters

8 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

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

10 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

11 Comments C-like comments One-line comments /* Lines of text */
! Line of text

12 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

13 Examples define(a2, 1) ^ no blanks
Preprocessor substitutes 1 for every occurrence of a2

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

15 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

16 Example Marking a function .global main main: save %sp, -64, %sp

17 Program Structure Introductory Comments Constants and defines
Storage for static, global variables Function name definition using .global Function body Function return

18 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

19 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

20 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

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

22 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 !**************************************************

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

24 ex02.6.m (3) ! void main() global main main: save %sp, -64, %sp

25 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 * o1 nop sub x_r, a0, %o1 ! o1 = x_r - a0 call .div ! o0 = o0 / o1 nop mov %o0, y_r ! y_r = o0

26 ex02.6.m (5) ! exit(0) mov 0, %o0 call exit nop ! mov 1, %g1 ! ta 0

27 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

28 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 * o1 sub x_r, a1, %o1 ! o1 = x_r - a1 sub x_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

29 Ex02.6.m revisited (2) ! call exit(0) call exit mov 0, %o0

30 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

31 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

32 Program Address Space Code Section Code static variables OS memory
Heap Section dynamic variables Stack Section automatic variables

33 Code section Contains storage for Code Operating System information
Static variables – global and local

34 Stack section Contains automatic variables of the functions
Contains frame information for each call of a function

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

36 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

37 Integer Variables Int comes in three flavors Examples in C short .byte
int .half long .word Examples in C short sum = 0; int vecSize = 5; long i = -1;

38 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

39 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”;

40 Assembler equivalent .align 4 prompt: .asciz “Enter an integer: “
message: .asciz “Too much data\n”

41 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

42 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

43 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

44 Compute sum + x; Code to compute sum + x set sum, %o1 ! o1 = addr(sum)
ld [%o1], %o1 ! o1 = sum set x, %o ! o2 = addr(x) ld [%o2], %o2 ! o2 = x add %o1, %o2, %o2 ! o2 = sum + x

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

46 Printing a Message (1) ! string message = “Hello, world\n”;
message: .asciz “Hello, world\n” ! printf(message); set message, %o0 call printf nop

47 Printing a Message (2) sethi %hi(message), %o0 call printf
or %o0, %lo(message), %o0 delay slot

48 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

49 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

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

51 scanf () example x: .word 0 fmti: .asciz “%d “ … !scanf(fmti, &x);
set fmti, %o0 set x, %o1 call scanf nop

Download ppt "SPARC Architecture & Assembly Language"

Similar presentations

Ads by Google