Writing 32-Bit Applications

Similar documents
Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 10. Advanced Procedures

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

EECE416 :Microcomputer Fundamentals and Design. X86 Assembly Programming Part 1. Dr. Charles Kim

Chapter 11. Addressing Modes

Lab 2: Introduction to Assembly Language Programming

Lab 3: Defining Data and Symbolic Constants

Machine and Assembly Language Principles

Load Effective Address Part I Written By: Vandad Nahavandi Pour Web-site:

Low Level Programming Lecture 2. International Faculty of Engineerig, Technical University of Łódź

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Assembly Language Programming: Procedures. EECE416 uc. Charles Kim Howard University. Fall

6/20/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

A4 Sample Solution Ch3

Basic Execution Environment

MICROPROCESSOR MICROPROCESSOR ARCHITECTURE. Prof. P. C. Patil UOP S.E.COMP (SEM-II)

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

16.317: Microprocessor Systems Design I Fall 2014

Chapter 3: Addressing Modes

MICROPROCESSOR ARCHITECTURE

ORG ; TWO. Assembly Language Programming

Marking Scheme. Examination Paper Department of CE. Module: Microprocessors (630313)

Machine Code and Assemblers November 6

MICROPROCESSOR TECHNOLOGY

COMPUTER ENGINEERING DEPARTMENT

Inline Assembler. Willi-Hans Steeb and Yorick Hardy. International School for Scientific Computing

Reverse Engineering II: Basics. Gergely Erdélyi Senior Antivirus Researcher

We can study computer architectures by starting with the basic building blocks. Adders, decoders, multiplexors, flip-flops, registers,...

Scott M. Lewandowski CS295-2: Advanced Topics in Debugging September 21, 1998

ICS143A: Principles of Operating Systems. Midterm recap, sample questions. Anton Burtsev February, 2017

Dr. Ramesh K. Karne Department of Computer and Information Sciences, Towson University, Towson, MD /12/2014 Slide 1

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

UMBC. contain new IP while 4th and 5th bytes contain CS. CALL BX and CALL [BX] versions also exist. contain displacement added to IP.

Ethical Hacking. Assembly Language Tutorial

SOEN228, Winter Revision 1.2 Date: October 25,

Reverse Engineering II: The Basics

Introduction to IA-32. Jo, Heeseung

Assembler Programming. Lecture 10

INTRODUCTION TO IA-32. Jo, Heeseung

16.317: Microprocessor Systems Design I Spring 2015

Addressing Modes on the x86

16.317: Microprocessor Systems Design I Fall 2013

/ 28 HLL assembly Q4: Conditional instructions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

16.317: Microprocessor Systems Design I Fall 2015

x86 assembly CS449 Fall 2017

Macros in Pentium. Assembly Language. October 25

Module 3 Instruction Set Architecture (ISA)

For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

The Instruction Set. Chapter 5

Hardware and Software Architecture. Chapter 2

MICROPROCESSOR MICROPROCESSOR ARCHITECTURE. Prof. P. C. Patil UOP S.E.COMP (SEM-II)

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 2: IA-32 Processor Architecture Included elements of the IA-64 bit

Complex Instruction Set Computer (CISC)

UNIVERSITY OF CALIFORNIA, RIVERSIDE

ELEC 242 Time Delay Procedure

Addressing Modes. Outline

EEM336 Microprocessors I. The Microprocessor and Its Architecture

Assembler Programming. Lecture 2

Assembly Language. Lecture 2 - x86 Processor Architecture. Ahmed Sallam

The Microprocessor and its Architecture

W4118: PC Hardware and x86. Junfeng Yang

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 5: Procedures. Chapter Overview. The Book's Link Library

Assembly Language Programming

Marking Scheme. Examination Paper. Module: Microprocessors (630313)

Assembly Language. Lecture 2 x86 Processor Architecture

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

Q. State and Explain steps involved in program development. [w-08, w-10, s-12, w-11]

COMPUTER ENGINEERING DEPARTMENT

Reverse Engineering II: The Basics

Using MMX Instructions to Perform Simple Vector Operations

Using MMX Instructions to Implement 2D Sprite Overlay

6/17/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

Marking Scheme. Examination Paper. Module: Microprocessors (630313)

Computer Systems Lecture 9

Computer Processors. Part 2. Components of a Processor. Execution Unit The ALU. Execution Unit. The Brains of the Box. Processors. Execution Unit (EU)

Chapter 2: The Microprocessor and its Architecture

Proposed Common Configuration Method

MODE (mod) FIELD CODES. mod MEMORY MODE: 8-BIT DISPLACEMENT MEMORY MODE: 16- OR 32- BIT DISPLACEMENT REGISTER MODE

Assembly Language for Intel-Based Computers, 4 th Edition. Lecture 25: Interface With High-Level Language

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

Assembly Language Each statement in an assembly language program consists of four parts or fields.

Title: Win Vista DLL Injection (32bit)

EECE.3170: Microprocessor Systems Design I Summer 2017

We will first study the basic instructions for doing multiplications and divisions

Tutorial 10 Protection Cont.

Static Analysis I PAOLO PALUMBO, F-SECURE CORPORATION

EEM336 Microprocessors I. Addressing Modes

Practical Malware Analysis

Philadelphia University Student Name: Student Number:

EEM336 Microprocessors I. Data Movement Instructions

Faculty of Engineering Student Number:

Summary: Direct Code Generation

Systems Architecture I

Assembly Language: Function Calls

Memory Organization. 27 December 2016 Pramod Ghimire. Slide 1 of 21

16.317: Microprocessor Systems Design I Spring 2014

x86 assembly CS449 Spring 2016

Assembly Language: Function Calls" Goals of this Lecture"

Experiment 8 8 Subroutine Handling Instructions and Macros

Transcription:

335 CHAPTER 13 Writing 32-Bit Applications This chapter is an introduction to 32-bit programming for the 80386. The guidelines in this chapter also apply to the 80486 processor, which is basically a faster 80386 with the equivalent of a 80387 floating-point processor. Since you are already familiar with 16-bit real-mode programming, this chapter covers the differences between 16-bit programming and 32-bit protected-mode programming. The 80386 processor (and its successors such as the 80486) can run in real mode, virtual-86 mode, and in protected mode. In real and virtual-86 modes, the 80386 can run 8086/8088 programs. In protected mode, it can run 80286 programs. The 386 also extends the features of protected mode to include 32-bit operations and segments larger than 64K. The MS-DOS operating system directly supports 8086/8088 programs, which it runs either in real mode or virtual-86 mode. Native 32-bit 80386 programs can be run by using a DOS extender, by using the WINMEM32.DLL facility of Microsoft Windows 3.x, or by running a native 32-bit operating system, such as Microsoft Windows NT. You can use MASM to generate object code (OMF or COFF) for 32-bit programs. To do this, you will need a software development kit such as the Windows SDK for the target environment. Such kits include the linker and other components specific to your chosen operating environment. 32-Bit Memory Addressing The 80386 has six segment registers. Four of these are familiar to 8086/8088 programmers: CS (Code Segment), SS (Stack Segment), DS (Data Segment), and ES (Extra Segment). The two additional registers, FS and GS, are used as data segment registers. Memory addresses on 80x86 machines consist of two parts a segment and an offset. In real-mode programs, the segment is a 16-bit number and the offset is a 16-bit number. Effective addresses are calculated by multiplying the segment by Revision #: 27 Page: 335 of 1 Printed: 10/02/00 04:21 PM

336 Programmer s Guide 16 and adding the offset to it. In protected mode, the segment value is not used directly as a number, but instead is an index to a table of selectors. Each selector describes a block of memory, including attributes such as the size and location of the block, and the access rights the program has to it (read, write, execute). The effective address is calculated by adding the offset to the base address of the memory block described by the selector. All segment registers are 16 bits wide. The offset in a 32-bit protected-mode program is itself 32 bits wide, which means that a single segment can address up to 4 gigabytes of memory. Because of this large range, there is little need to use segment registers to extend the range of addresses in 32-bit programs. If all six segment registers are initially set to the same value, then the rest of the program can ignore them and treat the processor as if it used a 32-bit linear address space. This is called 0:32, or flat, addressing. (The full segmented 32-bit addressing mode, in which the segment registers can contain different values, is called 16:32 addressing.) Flat addressing is used by the Windows NT operating system. Figure 13.1 32-Bit Register Set MASM Directives for 32-Bit Programming If you use the simplified segment directives, a 32-bit program is surprisingly similar to a program for MS-DOS. Here are the differences: Revision #: 27 Page: 336 of 2 Printed: 10/02/00 04:21 PM

Chapter 13 Writing 32-Bit Applications 337 Supply the.386 directive, which enables the 32-bit programming features of the 386 and its successors. The.386 directive must precede the.model directive. For flat-model programming, use the directive.model flat, stdcall which tells the assembler to assume flat model (0:32) and to use the Windows NT standard calling convention for subroutine calls. Precede your data declarations with the.data directive. Precede your instruction codes with the.code directive. At the end of the source file, place an END directive. Sample Program The following sample is a 32-bit assembly language subroutine, such as might be called from a 32-bit C program written for the Windows NT operating system. The program illustrates the use of a variety of directives to make assembly language easier to read and maintain. Note that with 32-bit flat model programming, there is no longer any need to refer to segment registers, since these are artifacts of segmented addressing. Revision #: 27 Page: 337 of 3 Printed: 10/02/00 04:21 PM

338 Programmer s Guide ;* szsearch - An example of 32-bit assembly programming using MASM 6.1 ;* ;* Purpose: Search a buffer (rgbsearch) of length cbsearch for the ;* first occurrence of sztok (null terminated string). ;* ;* Method: A variation of the Boyer-Moore method ;* 1. Determine length of sztok (n) ;* 2. Set array of flags (rgfintok) to TRUE for each character ;* in sztok ;* 3. Set current position of search to rgbsearch (pbcur) ;* 4. Compare current position to sztok by searching backwards ;* from the nth position. When a comparison fails at ;* position (m), check to see if the current character ;* in rgbsearch is in sztok by using rgfintok. If not, ;* set pbcur to pbcur+(m)+1 and restart compare. If ;* pbcur reached, increment pbcur and restart compare. ;* 5. Reset rgfintok to all 0 for next instantiation of the ;* routine..386.model flat, stdcall FALSE EQU 0 TRUE EQU NOT FALSE Revision #: 27 Page: 338 of 4 Printed: 10/02/00 04:21 PM

Chapter 13 Writing 32-Bit Applications 339.DATA ; Flags buffer - data initialized to FALSE. We will ; set the appropriate flags to TRUE during initialization ; of szsearch and reset them to FALSE before exit. rgfintok BYTE 256 DUP (FALSE);.CODE PBYTE TYPEDEF PTR BYTE szsearch PROC PUBLIC USES esi edi, rgbsearch:pbyte, cbsearch:dword, sztok:pbyte ; Initialize flags buffer. This tells us if a character is in ; the search token - Note how we use EAX as an index ; register. This can be done with all extended registers. mov esi, sztok xor eax, eax.repeat lodsb mov BYTE PTR rgfintok[eax], TRUE.UNTIL (!AL) ; Save count of sztok bytes in EDX mov edx, esi sub edx, sztok dec edx ; ESI will always point to beginning of sztok mov esi, sztok ; EDI will point to current search position ; and will also contain the return value mov edi, rgbsearch ; Store pointer to end of rgbsearch in EBX mov ebx, edi add ebx, cbsearch sub ebx, edx Revision #: 27 Page: 339 of 5 Printed: 10/02/00 04:21 PM

340 Programmer s Guide ; Initialize ECX with length of sztok mov ecx, edx.while ( ecx!= 0 ) dec ecx ; Move index to current mov al, [edi+ecx] ; characters to compare ; If the current byte in the buffer doesn't exist in the ; search token, increment buffer pointer to current position ; +1 and start over. This can skip up to 'EDX' ; bytes and reduce search time..if!(rgfintok[eax]) add edi, ecx inc edi ; Initialize ECX with mov ecx, edx ; length of sztok ; Otherwise, if the characters match, continue on as if ; we have a matching token.elseif (al == [esi+ecx]).continue ; Finally, if we have searched all sztok characters, ; and land here, we have a mismatch and we increment ; our pointer into rgbsearch by one and start over..elseif (!ecx) inc edi mov ecx, edx.endif ; Verify that we haven't searched beyond the buffer..if (edi > ebx) mov edi, 0 ; Error value.break.endif.endw ; Restore flags in rgfintok to 0 (for next time). mov esi, sztok xor eax, eax.repeat lodsb mov BYTE PTR rgfintok[eax], FALSE.UNTIL!AL ; Put return value in eax mov eax, edi ret szsearch ENDP end Revision #: 27 Page: 340 of 6 Printed: 10/02/00 04:21 PM

340 Programmer s Guide Revision #: 27 Page: 340 of 8 Printed: 10/02/00 04:21 PM