SPARC Architecture & Assembly Language

Slides:



Advertisements
Similar presentations
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Advertisements

1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
The University of Adelaide, School of Computer Science
Lecture 6 Programming the TMS320C6x Family of DSPs.
1 ARM Movement Instructions u MOV Rd, ; updates N, Z, C Rd = u MVN Rd, ; Rd = 0xF..F EOR.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Chapter 2 Machine Language.
I/O: SPARC Assembly Department of Computer Science Georgia State University Georgia State University Updated Spring 2014.
SPIM and MIPS programming
Fall EE 333 Lillevik 333f06-l4 University of Portland School of Engineering Computer Organization Lecture 4 Assembly language programming ALU and.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson slides3.ppt Modification date: March 16, Addressing Modes The methods used in machine instructions.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
1 Computer Architecture MIPS Simulator and Assembly language.
The University of Adelaide, School of Computer Science
1 COMS 361 Computer Organization Title: Instructions Date: 9/28/2004 Lecture Number: 10.
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
Sparc Architecture Overview
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
1/2002JNM1 Basic Elements of Assembly Language Integer Constants –If no radix is given, the integer is assumed to be decimal. Int 21h  Int 21 –A hexadecimal.
CSC 3210 Computer Organization and Programming Chapter 2 SPARC Architecture Dr. Anu Bourgeois 1.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Programming With C.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
IFT 201: Unit 1 Lecture 1.3: Processor Architecture-3
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
CSC 3210 Computer Organization and Programming Chapter 2 SPARC ARCHITECTURE D.M. Rasanjalee Himali.
1 The Instruction Set Architecture September 27 th, 2007 By: Corbin Johnson CS 146.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
MIPS Architecture Topics –What resources MIPS assembly manipulates –CPU (Central Processing Unit) –ALU (Arithmetic & Logical Unit), Registers –Memory –I/O.
Stacks and Frames Memory Stacks Frames Automatic Variables.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Lecture 3 Translation.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2 Introduction to C++ Programming
Introduction to the C Language
Chapter 2 - Introduction to C Programming
The Stack Chapter 5 Lecture notes for SPARC Architecture, Assembly Language Programming and C, Richard P. Paul by Anu G. Bourgeois.
Basic Elements of C++.
ICS103 Programming in C Lecture 3: Introduction to C (2)
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Computer Architecture and Organization Miles Murdocca and Vincent Heuring Chapter 4 – The Instruction Set Architecture.
Chapter 2 - Introduction to C Programming
Basic Elements of C++ Chapter 2.
Instructions - Type and Format
Ken D. Nguyen Department of Computer Science Georgia State University
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
CSC 3210 Computer Organization and Programming
Chapter 2 - Introduction to C Programming
The University of Adelaide, School of Computer Science
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Generalities for Assembly Language
Ken D. Nguyen Department of Computer Science Georgia State University
Introduction to C Programming
Week 5 Computers are like Old Testament gods; lots of rules and no mercy. Joseph Campbell.
Presentation transcript:

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 One-line comments /* Lines of text */ ! 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 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

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

ex02.6.m (5) ! exit(0) mov 0, %o0 call exit nop ! 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 * 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

Ex02.6.m revisited (2) ! call exit(0) call exit mov 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 Examples in C 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: “ 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.

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