Practical Session 4.

Slides:



Advertisements
Similar presentations
There are two types of addressing schemes:
Advertisements

COMP 2003: Assembly Language and Digital Logic
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Computer Organization And Assembly Language
Practical Session 3 Computer Architecture and Assembly Language.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
Accessing parameters from the stack and calling functions.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
CDP ECE Spring 2000 ECE 291 Spring 2000 Lecture 7: More on Addressing Modes, Structures, and Stack Constantine D. Polychronopoulos Professor, ECE.
Stack Operations LIFO structure (last-in,first-out) –The last value put into the stack is the first value taken out Runtime stack –A memory array that.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
CET 3510 Microcomputer Systems Tech. Lecture 2 Professor: Dr. José M. Reyes Álamo.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#9) By Dr. Syed Noman.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
Assembly 07. Outline Boxes within Boxes Procedure Definition call, ret Saving / Restoring Registers Argument(s) Return Value(s) Global vs. Local Data.
1 The Stack and Procedures Chapter 5. 2 A Process in Virtual Memory  This is how a process is placed into its virtual addressable space  The code is.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
Computer Architecture and Assembly Language
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Assembly Language Data Movement Instructions. MOV Instruction Move source operand to destination mov destination, source The source and destination are.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Practical Session 3 Computer Architecture and Assembly Language.
Practical Session 3.
Practical Session 5.
Stack Operations Dr. Hadi AL Saadi.
Reading Condition Codes (Cont.)
Computer Architecture and Assembly Language
Assembly language.
C function call conventions and the stack
Computer Architecture and Assembly Language
Computer Architecture and Assembly Language
Chapter 4 Data Movement Instructions
Introduction to Compilers Tim Teitelbaum
Assembly IA-32.
Assembly Language Programming Part 2
Practical Session 7.
CS 301 Fall 2002 Control Structures
Computer Architecture and Assembly Language
Y86 Processor State Program Registers
BIC 10503: COMPUTER ARCHITECTURE
Computer Architecture and Assembly Language
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
Practical Session 9.
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Multi-modules programming
3.6 Data transfer Instructions
Computer Architecture CST 250
X86 Assembly Review.
CSC 497/583 Advanced Topics in Computer Security
Computer Organization and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

Practical Session 4

The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items. Every cell in the stack is of size 2 or 4 bytes, never a single byte. The register ESP holds the address that points to the top of the stack (TOS). It is the lower byte of the last inserted data item. We use ESP as pointer and every cell is 2 or 4 bytes. stack in size of 2^32 bits.

Fucntion Call - Stack Frame Calling a function: Backup registers (optional) Push arguments in reverse order. Use the ‘call’ instruction to call the function. The return address will be pushed automatically onto the stack. Start of function: Backup the EBP (Base Pointer). Reset the EBP to the current ESP. Backup registers that are used in the function.

Fucntion Call - Stack Frame End of function: Restore backed up registers. Put return value in EAX (optional). Move EBP to ESP. Restore old EBP (by using ‘pop’). Use the ‘ret’ instruction to return. When returned from function Retrieve return value from EAX. Pop all arguments from the stack (or just add their size to ESP). Restore old registers (optional).

Stack Operations PUSH This instruction push data on stack. It decrements the stack pointer ESP by 2 or 4, and then stores the given value at ESP. The data pushed into the highest address of the stack, in little endian order. The size of the operand determine whether the stack pointer is decremented by 2 or 4. Example push the content of ax to stack in little endian order: mov ax, 0x21AB push ax 0xFF…F 21 ESP AB 0x00…0

PUSHAx This instruction Push All General-Purpose Registers. PUSHAD pushes, in succession, EAX, ECX, EDX, EBX, ESP, EBP, ESI and EDI on the stack, decrementing the stack pointer by a total of 32. In both cases, the value of ESP pushed is its original value, as it had before the instruction was executed. PUSHAW is an alias mnemonic for PUSHAD, for word-sized registers. PUSHA uses register-size definition according to the used (compiled) architecture. This means, compiling for 32 bit will enforce pusha==pushad Note that the registers are pushed in order of their numeric values in opcodes. PUSHFx PUSHFD pushes the entire flags register onto the stack. PUSHF is an alias for PUSHFD.

POP POP loads a value from the stack (from ESP) and then increments the stack pointer (by 2 or 4). The size of the operand determine whether the stack pointer is incremented by 2 or 4. Example mov ax, 3 push ax mov bx, 0x12AF push bx pop ax ; ax = 0x12AF pop bx ; bx = 3

POPAx Pop All General-Purpose Registers. POPAD pops a dword from the stack into each of, successively, EDI, ESI, EBP, nothing (placeholder for ESP), EBX, EDX, ECX and EAX. It reverses the operation of PUSHAD. POPA is an alias for POPAD. POPFx Pop Flags Register. POPFD pops a dword and stores it in the entire flags register. POPF is an alias for POPFD.

Code Examples

1.Reading a string Suppose ECX points to the first byte of a string. We wish to read the string character by character: read_string: mov al, byte[ecx] ; Read a single byte from the string. Store into a byte size ; register Do whatever processing you want on the character. inc ecx ; Increase the pointer address by one, moving it to the next ; character in the string mov ah, byte[ecx] ; Read the next character in the string cmp ah, 10 ; Compare AH to line_feed character jne read_string ; If not the end of the string, jump back to the beginning of ; the loop to continue reading the string Finishing code

2.my_func(short x)=x2 section .rodata LC0: DB "The result is: %d", 10, 0 section .text align 16 global my_func extern printf my_func: push ebp mov ebp, esp push ecx mov ax, [ebp+8] imul ax mov ecx, 0 mov cx, dx shl ecx, 16 mov cx, ax push LC0 call printf add esp, 4 pop eax pop ecx mov esp, ebp pop ebp ret

Assignment 1 Implementing a “long-long calculation machine”. A main function, written in C (given to you), loops forever over: get operation from console 1.1. If(input != 'q') call calc(operand1,operation,operand2) 1.2. Else quit

calc(operand1,operation,operand2) Assignment 1 Your part: A function: calc(operand1,operation,operand2) Input types are “strings”. Operands are representable in 16-bytes (i.e. 4-dwords) each. Operation is one of: + , - , * , / All of the code implemented by you must be in assembly. Can use ‘printf’ to print- no other library functions are allowed!

Assignment 1 – implementation key points How do I find the parameters given to me from c? How do I pass/receive parameters when calling/implementing a method? How do I turn ascii values into numeric ones? How do I manage my memory? How do I calculate an arithmetic operation, on a data element – larger than by largest register?