Natawut NupairojAssembly Language1 Memory and Stack.

Slides:



Advertisements
Similar presentations
Ch. 7 Local Variables and Parameter Passing From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.
Advertisements

Chapter 6 Data Structures
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Procedure Calls Prof. Sirer CS 316 Cornell University.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
SPARC Architecture & Assembly Language
I/O: SPARC Assembly Department of Computer Science Georgia State University Georgia State University Updated Spring 2014.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 2 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
9/20/6Lecture 3 - Instruction Set - Al Instruction Set.
Computer Architecture CSCE 350
1 Computer Architecture MIPS Simulator and Assembly language.
The University of Adelaide, School of Computer Science
Procedure call frame: Hold values passed to a procedure as arguments
Chapter 7: Subroutines Lecture notes to accompany the text book SPARC Architecture, Assembly Language Programming, and C, by Richard P. Paul, 2 nd edition,
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
Intro to Computer Architecture
RISC Concepts, MIPS ISA and the Mini–MIPS project
Sparc Architecture Overview
1 CSC 3210 Computer Organization and Programming Chapter 7 SUBROUTINES D.M. Rasanjalee Himali.
CSC 3210 Computer Organization and Programming Chapter 9 EXTERNAL DATA AND TEXT D.M. Rasanjalee Himali.
Natawut NupairojAssembly Language1 Subroutines. Natawut NupairojAssembly Language2 Subroutines A subroutine is a piece of program codes that performs.
MIPS R3000 Subroutine Calls and Stack Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
CSC 3210 Computer Organization and Programming Chapter 8 MACHINE INSTRUCTIONS D.M. Rasanjalee Himali.
CSC 3210 Computer Organization and Programming Chapter 5 THE STACK D.M. Rasanjalee Himali.
Natawut NupairojAssembly Language1 Arithmetic Operations.
Control Transfer and Arithmetic Conditional/Unconditional branches Delayed Control Transfer –Increases the efficiency of pipelining Annulled branches.
Natawut NupairojAssembly Language1 Control Structure.
V 1.01 Arrays and Pointers in C A pointer variable is a variable that contains the address of another variable. An array is a collection of like elements,
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
Functions in Assembly CS 210 Tutorial 7 Functions in Assembly Studwww.cs.auckland.ac.nz/ ~mngu012/public.html/210/7/
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 8 – Machine Instructions These are lecture notes to accompany the book.
1 The Instruction Set Architecture September 27 th, 2007 By: Corbin Johnson CS 146.
Natawut NupairojAssembly Language1 Pipelining Processor.
4-1 Embedded Systems C Programming Language Review and Dissection II Lecture 4.
Accessing Memory Chapter 5 Lecture notes for SPARC Architecture, Assembly Language Programming and C, Richard P. Paul by Anu G. Bourgeois.
EECS 370 Discussion 1 xkcd.com. EECS 370 Discussion Topics Today: – ARM Addressing Endianness, Loading, and Storing Data – Data Layout Struct Packing.
The Stack Chapter 5 Lecture notes for SPARC Architecture, Assembly Language Programming and C, Richard P. Paul by Anu G. Bourgeois, Abinashi Dhungel.
Stacks and Frames Memory Stacks Frames Automatic Variables.
©SoftMoore ConsultingSlide 1 The CPRL Virtual Machine.
Chapter 8 – Machine Instructions
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Lecture 5: Procedure Calls
Format of Assembly language
The Stack.
The Stack Chapter 5 Lecture notes for SPARC Architecture, Assembly Language Programming and C, Richard P. Paul by Anu G. Bourgeois.
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Chapter 7 Subroutines Dr. A.P. Preethy
Symbolic Instruction and Addressing
8086 Registers Module M14.2 Sections 9.2, 10.1.
CS-401 Computer Architecture & Assembly Language Programming
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
MIPS Instructions.
Chapter 8 Central Processing Unit
The University of Adelaide, School of Computer Science
Symbolic Instruction and Addressing
A Closer Look at Instruction Set Architectures Chapter 5
Computer Instructions
Procedures and Calling Conventions
Computer Architecture and System Programming Laboratory
Chapter 6 –Symbolic Instruction and Addressing
CSC 497/583 Advanced Topics in Computer Security
MIPS R3000 Subroutine Calls and Stack
Part I Data Representation and 8086 Microprocessors
An Introduction to the ARM CORTEX M0+ Instructions
Chapter 10 Instruction Sets: Characteristics and Functions
Presentation transcript:

Natawut NupairojAssembly Language1 Memory and Stack

Natawut NupairojAssembly Language2 Memory In SPARC, there are only 32 registers. Not enough to hold all data for computation. We use memory to store variables. Variables in memory can be: CSPARCbits charbyte 8 shorthalfword16 int, longword32

Natawut NupairojAssembly Language3 Memory All variables in memory must be “aligned”. –A “short” variable is 16-bit long or halfword (2 bytes) –It must be stored in the addressed that are divisible by “two”, or “even address” (aligned to two-byte boundary) –For example, 0, 2, 4, …, 1024, 1026, etc. –An “int” variable is 32-bit long or one word (4 bytes) –It must be stored in the addressed that are divisible by “four”. (aligned to four-byte boundary). –For example, 0, 4, 8, …, 1024, 1028, etc. This is for efficiency in hardware.

Natawut NupairojAssembly Language4 Memory The SPARC architecture is big endian. Store LSB (the smallest-numbered byte) at the first address. For example: to store a short variable containing 0x0932 at address 1026 (must be aligned!) 0x32 (LSB) is stored at address x09 (MSB) is stored at address Note: an instruction must be word-aligned. Why ?

Natawut NupairojAssembly Language5 The Stack We store automatic or “local” variables in the memory called “ Stack ”. An automatic variable is a variable that is accessible only inside a function. int g; int main() { int i, j;... } Global var Local vars

Natawut NupairojAssembly Language6 The Stack Pointer The stack is last-in-first-out (LIFO). Each program has its own private stack. %o6 aka. %sp is the stack pointer. Stack is located near the top of memory (biggest addressed). When the stack grows, the %sp decreases. When the stack shrinks, the %sp increases. Thus to get more spaces in the stack, we subtract the number of bytes from the stack pointer.

Natawut NupairojAssembly Language7 The Stack Pointer To get 64 bytes more: –Sub %sp, 64, %sp

Natawut NupairojAssembly Language8 The Stack Pointer The stack must be doubleword (8-byte) aligned. Thus, the address must be divisible by eight. If we want 94 bytes, we must ask for 96 bytes to keep the stack aligned. Thus: sub %sp, 96, %sp Or we can: add %sp, -94 & -8, %sp Why -94&-8 ? Check out two’s complement. Done by assembler*

Natawut NupairojAssembly Language9 The Frame Pointer The stack pointer is always changed as more variables are needed. How can we refer to a variable ? Use the frame pointer, %fp or %i6. The frame pointer remains fixed for each subroutine. At the beginning of the program, we execute a “save” instruction to allocate space in the stack.

Natawut NupairojAssembly Language10 Frame and Stack Pointers Subroutine A calls B: int A() {... B();... } int B() {... } Before Call After Call

Natawut NupairojAssembly Language11 Save Instruction The save instruction must allocate space for both local variables and registers. Must allocate 64 bytes + spaces for variables. save %sp, -64-bytes_for_vars, %sp Suppose we want to store five “int” (4-byte) variables (var0 - var4): save %sp, (-64-(5*4)) & -8, %sp This is actually: save %sp, -88, %sp

Natawut NupairojAssembly Language12 Save Instruction

Natawut NupairojAssembly Language13 Addressing Stack Variables As SPARC is the load-store architecture, we cannot compute variables data from the stack directly. We must load them to registers, compute, and then store back to the stack. Remember all variables must be aligned based on its size. SPARC has different load/store instructions for each type.

Natawut NupairojAssembly Language14 Load Instructions ldsb - load signed byte, propagate sign. ldub - load unsigned byte, clear high 24 bits of register. ldsh - load signed halfword, propagate sign. lduh - load unsigned halfword, clear high 16 bits of register. ld - load word ldd - load double, register number even, first four bytes into register n, next four into register n+1.

Natawut NupairojAssembly Language15 Load Instructions ld [%fp - 4], %l1! Load var0 into %l1 ld [%fp - 8], %o2! Load var1 into %o2 mov -16, %l4 ld [%fp + %l4], %l3! Load var3 into %l3 ldd [%fp - 16], %g2! Load var3 into %g2 ! and var2 into %g3 ldd [%fp - 16], %l5! Illegal, why ?

Natawut NupairojAssembly Language16 Store Instructions stb - store low byte of register, bits into memory. sth - store low two bytes of register, bits into memory. st - store register. std - store double, register number even, first four bytes from register n, next four from register n+1.

Natawut NupairojAssembly Language17 Store Instructions st %l1, [%fp - 4]! Store %l1 into var0 st %o2, [%fp - 8]! Store %o2 into var1 sth %l4, [%fp - 6]! Store halfword of %l4 sth %l4, [%fp - 9]! Illegal, why ? st %o2, [%fp %l2]! Illegal, why ? st %o2, [%fp ]! Illegal, why ?

Natawut NupairojAssembly Language18 Variable Offsets in Stack We use the frame pointer as the base reference to variables in the stack. All variables must be properly aligned. Example: int a, b;// 4 bytes each char ch;// 1 byte short c, d;// 2 bytes each unsigned e;// 4 bytes

Natawut NupairojAssembly Language19 Variable Offsets in Stack a: %fp - 4 b: %fp - 8 ch: %fp - 9 c: %fp - 12 d: %fp - 14 e: %fp - 20

Natawut NupairojAssembly Language20 Actual Addresses

Natawut NupairojAssembly Language21 Offsets and Stack Allocation Use macro to arrange the offsets define(a_s, -4) define(b_s, -8) define(ch_s, -9) define(c_s, -12) define(d_s, -14) define(e_s, -20) Allocate spaces on stack: save %sp, (( ) & -8), %sp ==> save %sp, -84 & -8, %sp ==> save %sp, -88, %sp

Natawut NupairojAssembly Language22 Manipulate Variables in Stack To load and store ld [%fp + a_s], %l0 ldub [%fp + ch_s], %l1! char type is unsigned. ldsh [%fp + d_s], %l2! short type is signed. ld [%fp + e_s], %l3 To compute: b = a + c; ld [%fp+a_s], %l0 ldsh [%fp+c_s], %l1 add %l0, %l1, %l2 st %l2, [%fp+b_s]

Natawut NupairojAssembly Language23 Variables in Registers Some variables are used very often. –Loop counters We can use registers to hold their values instead of using stack. In C, we use a keyword “register”. register int i;// i is in a register. When referred to these variables, we use values from registers directly.

Natawut NupairojAssembly Language24 Variables in Registers int a, b; register int j, k; int x, y; Only a, b, x, and y are in the stack. j and k are in registers. define(a_s, -4) define(b_s, -8) define(x_s, -12) define(y_s, -16) define(j_r, l0) define(k_r, l1)

Natawut NupairojAssembly Language25 Variables in Registers To compute: j = 19; a = 8; y = j a; Note: we use %l2 and %l3 as temporary registers. mov 19, %j_r mov 9, %l2 st %l2, [%fp+a_s] sub %j_r, 3, %l2 ld %l3, [%fp+a_s] add %l2, %l3, %l2 st %l2, [%fp+y_s]

Natawut NupairojAssembly Language26 Our Fourth Program main() { int a, b, c; register int i; i = 0; a = 100; b = 15; c = 0; while(i < 20) { c += a - b; a--; i = i + 2; }

Natawut NupairojAssembly Language27 Our Fourth Program define(a_s, -4) define(b_s, -8) define(c_s, -12) define(i_r, l0).global main main:save %sp, ( ) & -8, %sp clr %i_r! i = 0; mov 100, %l1 st %l1, [%fp + a_s]! a = 100; mov 15, %l1 st %l1, [%fp + b_s]! b = 15;

Natawut NupairojAssembly Language28 Our Fourth Program clr %l1 st %l1, [%fp + c_s]! c = 0; loop:cmp %i_r, 20 bge done nop ld [%fp + a_s], %l1 ld [%fp + b_s], %l2 sub %l1, %l2, %l3! a - b ld [%fp + c_s], %l1 add %l1, %l3, %l1! c + a - b st %l1, [%fp + c_s]! c += a - b;

Natawut NupairojAssembly Language29 Our Fourth Program ld [%fp + a_s], %l1 sub %l1, 1, %l1 st %l1, [%fp + a_s]! a--; add %i_r, 2, %i_r! i = i + 2 ba loop nop done:mov 1, %g1 ta 0