8. Procedures. X86 Assembly Language Programming for the PC 71

Similar documents
Faculty of Engineering Student Number:

Mouse Programming Mouse Interrupts Useful Mouse functions Mouselib.h Mouselib.c

BCD (ASCII) Arithmetic. Where and Why is BCD used? Packed BCD, ASCII, Unpacked BCD. BCD Adjustment Instructions AAA. Example

8. MACROS, Modules, and Mouse

Unpacked BCD Arithmetic. BCD (ASCII) Arithmetic. Where and Why is BCD used? From the SQL Server Manual. Packed BCD, ASCII, Unpacked BCD

A Tiny Guide to Programming in 32-bit x86 Assembly Language

Complete 8086 instruction set

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

X86-64 Architecture Guide

Assembly Language: Function Calls" Jennifer Rexford!

COMPUTERS ORGANIZATION 2ND YEAR COMPUTE SCIENCE MANAGEMENT ENGINEERING JOSÉ GARCÍA RODRÍGUEZ JOSÉ ANTONIO SERRA PÉREZ

Computer Systems Architecture

Using Debug 1 INTRODUCING DEBUG

Computer Organization and Architecture

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

The 80x86 Instruction Set

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

Systems Design & Programming Data Movement Instructions. Intel Assembly

Hacking Techniques & Intrusion Detection. Ali Al-Shemery arabnix [at] gmail

Machine Programming II: Instruc8ons

x64 Cheat Sheet Fall 2015

MICROPROCESSOR BCA IV Sem MULTIPLE CHOICE QUESTIONS

Instruction Set Architecture

CHAPTER 6 TASK MANAGEMENT

Chapter 7D The Java Virtual Machine

ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER

Assembly Language Tutorial

8085 INSTRUCTION SET

Overview. CISC Developments. RISC Designs. CISC Designs. VAX: Addressing Modes. Digital VAX

Writing an 8086 emulator in Python

The x86 PC: Assembly Language, Design, and Interfacing 5 th Edition

THUMB Instruction Set

Computer Organization and Assembly Language

Typy danych. Data types: Literals:

PROBLEMS (Cap. 4 - Istruzioni macchina)

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

PART B QUESTIONS AND ANSWERS UNIT I

Instruction Set Architecture. or How to talk to computers if you aren t in Star Trek

How To Use A Computer With A Screen On It (For A Powerbook)

Outline. Lecture 3. Basics. Logical vs. physical memory physical memory. x86 byte ordering

Lecture 3 Addressing Modes, Instruction Samples, Machine Code, Instruction Execution Cycle

HC12 Assembly Language Programming

Z80 Instruction Set. Z80 Assembly Language

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

1 The Java Virtual Machine

CPU Organization and Assembly Language

Dongwoo Kim : Hyeon-jeong Lee s Husband

Embedded x86 Programming: Protected Mode

Chapter 4 Lecture 5 The Microarchitecture Level Integer JAVA Virtual Machine

EECS 427 RISC PROCESSOR

Instruction Set Architecture (ISA)

Buffer Overflows. Security 2011

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

MICROPROCESSOR AND MICROCOMPUTER BASICS

M A S S A C H U S E T T S I N S T I T U T E O F T E C H N O L O G Y DEPARTMENT OF ELECTRICAL ENGINEERING AND COMPUTER SCIENCE

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Question Bank Subject Name: EC Microprocessor & Microcontroller Year/Sem : II/IV

INTRODUCTION TO PROGRAMMING THE 8086

2) Write in detail the issues in the design of code generator.

Intel 8086 architecture

Keil C51 Cross Compiler

More MIPS: Recursion. Computer Science 104 Lecture 9

PC Assembly Language. Paul A. Carter

CHAPTER 7: The CPU and Memory

8051 hardware summary

How It All Works. Other M68000 Updates. Basic Control Signals. Basic Control Signals

Chapter 2 Topics. 2.1 Classification of Computers & Instructions 2.2 Classes of Instruction Sets 2.3 Informal Description of Simple RISC Computer, SRC

l C-Programming l A real computer language l Data Representation l Everything goes down to bits and bytes l Machine representation Language

Instruction Set Architecture (ISA) Design. Classification Categories

Software Vulnerabilities

Computer Systems Design and Architecture by V. Heuring and H. Jordan

8086 Microprocessor (cont..)

King Fahd University of Petroleum and Minerals. College of Computer Science and Engineering. Computer Engineering Department COE 205

Software Fingerprinting for Automated Malicious Code Analysis

64-Bit NASM Notes. Invoking 64-Bit NASM

5. Calling conventions for different C++ compilers and operating systems

CS61: Systems Programing and Machine Organization

Introduction to MIPS Assembly Programming

Chapter 9 Computer Design Basics!

Data Structures and Algorithms V Otávio Braga

Stack Allocation. Run-Time Data Structures. Static Structures

An Overview of Stack Architecture and the PSC 1000 Microprocessor

02 B The Java Virtual Machine

Call Subroutine (PC<15:0>) TOS, (W15)+2 W15 (PC<23:16>) TOS, Process data. Write to PC NOP NOP NOP NOP

Instruction Set Design

Overview of IA-32 assembly programming. Lars Ailo Bongo University of Tromsø

Embedded X86 Programming: Protected Mode

612 CHAPTER 11 PROCESSOR FAMILIES (Corrisponde al cap Famiglie di processori) PROBLEMS

Central Processing Unit Simulation Version v2.5 (July 2005) Charles André University Nice-Sophia Antipolis

Phoenix Technologies Ltd.

Administrative Issues

An Introduction to the ARM 7 Architecture

Section 44. CPU with Extended Data Space (EDS)

Modbus RTU Communications RX/WX and MRX/MWX

Operating Systems. Privileged Instructions

Guide to RISC Processors

Programmer s Model = model of µc useful to view hardware during execution of software instructions

MICROPROCESSOR. Exclusive for IACE Students iacehyd.blogspot.in Ph: /422 Page 1

esrever gnireenigne tfosorcim seiranib

Flash Microcontroller. Memory Organization. Memory Organization

Transcription:

8 Procedures X86 Assembly Language Programming for the PC 71

Stack Operation A stack is a region of memory used for temporary storage of information Memory space should be allocated for stack by the programmer The last value placed on the stack is the 1st to be taken off This is called LIFO (Last In, First Out) queue Values placed on the stack are stored from the highest memory location down to the lowest memory location SS is used as a segment register for address calculation together with SP X86 Assembly Language Programming for the PC 72

Stack Instructions Name Mnemonic and Description Format Push onto Stack push src (sp) (sp)-2 ((sp)) (src) Pop from Stack pop dst (dst) ((sp)) (sp) (sp)+2 Push Flags pushf (sp) (sp)-2 ((sp)) (psw) Pop Flags popf (psw) ((sp)) (sp) (sp)+2 Flags: Only affected by the popf instruction Addressing Modes: src & dst should be Words and cannot be immediate dst cannot be the ip or cs register X86 Assembly Language Programming for the PC 73

Exercise: Fill-in the Stack Stack: Initially: (ss) = F000, (sp)=0008 F0010 pushf F000E mov ax,2211h F000C push ax F000A add ax,1111h F0008 push ax F0006 F0004 F0002 F0000 pop cx pop ds popf X86 Assembly Language Programming for the PC 74

Procedure Definition PROC is a statement used to indicate the beginning of a procedure or subroutine ENDP indicates the end of the procedure Syntax: ProcedureName PROC Attribute ProcedureName ENDP ProcedureName may be any valid identifier Attribute is NEAR if the Procedure is in the same code segment as the calling program; or FAR if in a different code segment X86 Assembly Language Programming for the PC 75

Call and Return Instructions Name Intrasegment Direct Call Intrasegment Indirect Call Intersegment Direct Call Intersegment Indirect Call Intrasegment Return Intrasegment Return with immediate data Intersegment Return Intersegment Return with immediate data Mnemonic and Format call opr call opr call opr call opr ret ret expression ret ret expression Description (sp) (sp)-2 ((sp)) (ip) (ip) (ip)+16-bit Disp (sp) (sp)-2 ((sp)) (ip) (ip) (Eff Addr) (sp) (sp)-2 ((sp)) (cs) (sp) (sp)-2 ((sp)) (ip) (ip) 16-bit Disp (cs) Segment Address (sp) (sp)-2 ((sp)) (cs) (sp) (sp)-2 ((sp)) (ip) (ip) (EffAddr) (cs) (Eff Addr + 2) (ip) ((sp)) (sp) (sp)+2 (ip) ((sp)) (sp) (sp)+2 (sp) (sp)+expression (ip) ((sp)) (sp) (sp)+2 (cs) ((sp)) (sp) (sp)+2 (ip) ((sp)) (sp) (sp)+2 (cs) ((sp)) (sp) (sp)+2 X86 Assembly Language Programming for the PC 76

(sp) (sp)+expression Flags: Not affected Addressing Modes: Any branch addressing mode except short X86 Assembly Language Programming for the PC 77

EXAMPLE: model medium data vector1 dw action1 vector2 dd action2 code action1 proc near ret action1 endp action2 proc far ret action2 endp start: ;Intrasegment Direct call action1 ;Intrasegment Indirect call vector1 ;Intersegment Direct call action2 ;Intersegment Indirect call vector2 end start X86 Assembly Language Programming for the PC 78

Exercise: Fill-in the Stack Stack: (ss) = F000h, (sp)=0012h, (cs)=2000h, done=6050h F0022 F0020 mov ax,2211h F001E push ax F001C call junk F001A done: mov var1,ax F0018 F0016 F0014 F0012 F0010 (cs)=3000h, junk=8000h F000E junk proc far F000C push bp F000A F0008 F0006 F0004 pop bp F0002 ret 2 F0000 junk endp X86 Assembly Language Programming for the PC 79

Exercise Write a procedure named multiply that computes the product of two signed 16-bit operands The operands will be passed in registers si and di The procedure should return the result on ax Write a program that uses the multiply procedure X86 Assembly Language Programming for the PC 80

Procedure Parameters Few procedures perform activities without requiring some input parameters that can be passed: 1 in registers 2 in memory variables 3 on the stack By convention, high-level languages (like C, Pascal, PL/1, ect) pass parameters by placing them on the stack Parameter on the stack can be passed by Value or by Reference Passing by Value means to put a copy of each parameter value on the stack Passing by Reference means to put a copy of each parameter offset (effective address) on the stack Parameters on the stack can then be accessed by procedures by using displacements or a stack-frame structure X86 Assembly Language Programming for the PC 81

EXAMPLE: Passing Parameters model medium data var1 dw? var2 dw? code action1 proc near ret 4 action1 endp action2 proc near ret 4 action2 endp start: ;Pass by Value push var1 push var2 call action1 ;Pass by Reference push offset var1 push offset var2 call action2 end start X86 Assembly Language Programming for the PC 82

Using Displacement To access parameters from the stack, a marker to the stack frame is required BP & SP default to the stack if used as base registers BP is commonly used by procedures, but need to be pushed before Parameters are accessed at [BP+Disp] after a push of bp and a mov of SP to BP EXAMPLE: clear proc near Stack: push bp mov bp,sp [bp+6] offset var1 push bx [bp+4] offset var2 mov bx,[bp+4] [bp+2] caller ip mov word ptr [bx],0 [bp] saved bp mov bx,[bp+6] [bp-2] saved bx mov word ptr [bx],0 pop bx pop bp ret 4 clear endp main: push push call offset var1 offset var2 clear X86 Assembly Language Programming for the PC 83

Exercise Write a procedure named multiply that computes the product of two signed 16-bit operands The operands will be passed on the stack, by-value The procedure should return the result on ax Write a program that uses the multiply procedure X86 Assembly Language Programming for the PC 84

Using a Stack Frame Structure The stack frame structure can be used as a template over the stack Based addressing can be used after a push of bp and a mov of SP to BP The displacement is then from the structure definition (not memory allocation is required) EXAMPLE: stack_frame struc Stack: saved_bp dw? caller_ip dw? [bp+6] offset Var1 var2_ptr dw? [bp+4] offset Var2 var1_ptr dw? [bp+2] caller ip stack_frame ends [bp] saved bp [bp-2] saved bx clear proc near push bp mov bp,sp push bx mov bx,[bp]var2_ptr mov word ptr [bx],0 mov bx,[bp]var1_ptr mov word ptr [bx],0 pop bx pop bp ret 4 clear endp main: push push call offset Var1 offset Var2 clear X86 Assembly Language Programming for the PC 85

Procedure Variables Procedures often need local memory space The stack area can be used to allocate space dynamically for the procedure with the space de-allocated when the procedure concludes To allocate space for local variables, subtract from SP the number of bytes needed after setting-up the stack frame marker (BP) Then, local variables can be accessed at [BP-number] and the parameters at [BP+number] Local variables are released by moving BP back to SP (mov sp,bp) X86 Assembly Language Programming for the PC 86

Exercise: Fill-in the Stack junk proc near push bp mov bp,sp sub sp,4 ;allocate local variables push ax mov ax,[bp+4] ;parameter var2 mov [bp-2],ax ;local variable mov ax,[bp+6] ;parameter var1 mov [bp-4],ax ;local variable pop ax mov sp,bp pop bp ret 4 ;return & clean-up stack junk endp main: push var1 push var2 call junk Stack: F0010 F000E F000C F0008 F0006 F0004 F0002 F0000 Initially: (ss)=f000, (sp)=0010 [BP+6] [BP+4] [BP+2] [BP] [BP-2] [BP-4] X86 Assembly Language Programming for the PC 87

C-Language Interfacing C-Language passes parameters to a procedure on the stack from right-to-left order The calling program is responsible of cleaning up the stack The procedure is free to modify the following registers without preserving: AX, CX, DX Values are returned in the following registers: Returned Data Type Byte Word Double Word Register AL AX DX:AX X86 Assembly Language Programming for the PC 88

EXAMPLE: Calling ASM from C _add proc near void main() push bp { mov bp,sp total1 =_add(1,2); mov ax,[bp+4] add ax,[bp+6] total2 =_sub(3,4) pop bp } ret _add endp _sub proc near push bp mov bp,sp mov ax,[bp+4] sub ax,[bp+6] pop bp ret _sub endp X86 Assembly Language Programming for the PC 89

EXAMPLE: Calling C from ASM int _add(int a, int b) mov ax,2 { push ax return a + b; mov ax,1 } push ax call _add add sp,4 int _sub(int a, int b) mov total1,ax { return a - b; } mov ax,4 push ax mov ax,3 push ax call _sub add sp,4 mov total2,ax X86 Assembly Language Programming for the PC 90