This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
The x86 PCAssembly Language, Design, and InterfacingBy Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey
7.3: PASSING PARAMETERS AMONG MODULES passing parameters via registers• Parameters can be passed from one module to another through
registers, memory, or the stack. – Fixed values, variables, arrays of data, memory pointers.
• When there is a need to pass parameters among various modules, one could use CPU registers. – The programmer must clearly document the registers used for the incoming
data & registers expected tohave the result after the execution of the subroutine.
• The limited number of CPU registers a major limitation associated with this method of parameter passing.
The x86 PCAssembly Language, Design, and InterfacingBy Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey
7.3: PASSING PARAMETERS AMONG MODULES passing parameters via the stack
• The stack is a very critical part of every program.– Playing with it can be risky.
• When a module is called, the stack holds the return address, where the program returns after execution. – If the stack contents are altered, the program can crashcrash.
The x86 PCAssembly Language, Design, and InterfacingBy Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey
7.3: PASSING PARAMETERS AMONG MODULES passing parameters via the stack
• Program 7-8, page 212, demonstrates this method of parameter passing, written with the following requirements. – The main module gets three word-sized operands from
the data segment, stores them on the stack, and callsthe subroutine.
– The subroutine gets the operands from the stack, adds them together, holds the result in a register, and returns control to the main module.
– The main module stores the result of the addition. Adobe Acrobat
Îĵµ
The x86 PCAssembly Language, Design, and InterfacingBy Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey
7.3: PASSING PARAMETERS AMONG MODULES stack contents analysis for Program 7-8
• After all parameters are brought into the CPU by the present module & processed (in this case added), the module restores the original BPBP contents by POPPOPping BPBP from stack. – SP = 17F4.
The x86 PCAssembly Language, Design, and InterfacingBy Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey
7.3: PASSING PARAMETERS AMONG MODULES stack contents analysis for Program 7-8
• RET 6RET 6 is a new instruction. – "RET n" instruction means first to POP CS:IP(IP only if the CALL was NEAR) off the top of thestack and then add n to the SP.
– After popping CS and IP off the stack, the stack pointer is incremented four times, making SP = 17F8.
• Adding 6 to bypass the six locations of the stack where the parameters are stored makes SP = 17FEH, its original value.
The x86 PCAssembly Language, Design, and InterfacingBy Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey
7.3: PASSING PARAMETERS AMONG MODULES stack contents analysis for Program 7-8
• If the program had a RET instruction, instead of the "RET 6“, every time this subprogram is executed it will cause the stack to lose six locations. – If this practice of losing some area of the stack continues,
eventually the stack could be reduced to a point where the program would run out of stack and crash.
The x86 PCAssembly Language, Design, and InterfacingBy Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey
• In the 16-bit, register AXAX is accessible either as ALAL, AHAH or AXAX, while in the 32-bit, register EAXEAX can be accessed only as ALAL, AH AH, AX AX or EAX EAX.
The samerule appliesto EBX, ECX,and EDX.
The upper 16 bits ofEAXEAX are not accessibleas a separate register.
The x86 PCAssembly Language, Design, and InterfacingBy Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey
8.1: 32-BIT PROGRAMMING IN x86accessing 32-bit registers
• The Assembly language directive ".386" is used to access 32-bit registers of 386 and higher CPUs.– The ".386" directive means the program cannot run on
8086/286.– Additional assembler directives indicate the type of
microprocessor supported by (MASM):
The x86 PCAssembly Language, Design, and InterfacingBy Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey
8.1: 32-BIT PROGRAMMING IN x86 little endian revisited
• x86 stores 32-bit data in memory, or loads 32-bit operands into registers with little endian convention.– Low byte to low address; high byte to high address.
See the entire program listing on page 220 of your textbook.
An instruction such as"MOV RESULT,EAXMOV RESULT,EAX" will store the data inthis way:
The x86 PCAssembly Language, Design, and InterfacingBy Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey
8.1: 32-BIT PROGRAMMING IN x86 little endian revisited
• x86 stores 32-bit data in memory, or loads 32-bit operands into registers with little endian convention.– Low byte to low address; high byte to high address.
The x86 PCAssembly Language, Design, and InterfacingBy Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey
8.1: 32-BIT PROGRAMMING IN x86 combining C with Assembly
• There are two ways to mix C/C++ and Assembly. – One is simply to insert the Assembly code in C programs.
• Commonly referred to as in-line assembly.
– The second method is to make the C/C++ languagecall an external Assembly language procedure.
• The following code demonstrates how to change the cursor position to row = 10 & column = 20. – Assembly instructions are prefaced with "asm“, a reserved word.
• Microsoft uses the keyword "_asm".
The x86 PCAssembly Language, Design, and InterfacingBy Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey
• 8088 is a 40-pin microprocessor chip that can workin two modes: minimum mode and maximum mode. – Maximum mode is used to connect to 8087 coprocessor.
• If a coprocessor is not needed, 8088 is used in minimum mode.
The x86 PCAssembly Language, Design, and InterfacingBy Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey
• 8088 can access both memory and I/O devices for read and write operations, four operations, which need four control signals:– MEMR (memory read); MEMW (memory write).– IOR (I/O read); IOW (I/O write).
The x86 PCAssembly Language, Design, and InterfacingBy Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey