|
Introduction to 80x86 Assembly Language and Computer Architecture Authors: Detmer R. C. C. Published year: 2000 Pages: 65/113 |
| < Day Day Up > |
Macros in the file IO.H are designed to provide simple, safe access to standard input and output devices. Figure 9.13 shows the contents of IO.H and the remainder of the section discusses the directives and macros in the file.
|
|
; IO.H - header file for I/O macros ; 32-bit version for flat memory model ; R. Detmer last revised 8/2000 .NOLIST ; turn off listing .386 EXTRN itoaproc:near32, atoiproc:near32 EXTRN dtoaproc:near32, atodproc:near32 EXTRN inproc:near32, outproc:near32 itoa MACRO dest,source,xtra ;; convert integer to ASCII string IFB <source> .ERR <missing operand(s) in ITOA> EXITM ENDIF IFNB <xtra> .ERR <extra operand(s) in ITOA> EXITM ENDIF push ebx ;; save EBX mov bx, source push bx ;; source parameter lea ebx,dest ;; destination address push ebx ;; destination parameter call itoaproc ;; call itoaproc(source,dest) pop ebx ;; restore EBX ENDM atoi MACRO source,xtra ;; convert ASCII string to integer in AX ;; offset of terminating character in ESI IFB <source> .ERR <missing operand in ATOI> EXITM ENDIF IFNB <xtra> .ERR <extra operand(s) in ATOI> EXITM ENDIF push ebx ;; save EBX lea ebx,source ;; source address to EBX push ebx ;; source parameter on stack call atoiproc ;; call atoiproc(source) pop ebx ;; parameter removed by ret ENDM dtoa MACRO dest,source,xtra ;; convert double to ASCII string IFB <source> .ERR <missing operand(s) in DTOA> EXITM ENDIF IFNB <xtra> .ERR <extra operand(s) in DTOA> EXITM ENDIF push ebx ;; save EBX mov ebx, source push ebx ;; source parameter lea ebx,dest ;; destination address push ebx ;; destination parameter call dtoaproc ;; call dtoaproc(source,dest) pop ebx ;; restore EBX ENDM atod MACRO source,xtra ;; convert ASCII string to integer in EAX ;; offset of terminating character in ESI IFB <source> .ERR <missing operand in ATOD> EXITM ENDIF IFNB <xtra> .ERR <extra operand(s) in ATOD> EXITM ENDIF lea eax,source ;; source address to EAX push eax ;; source parameter on stack call atodproc ;; call atodproc(source) ;; parameter removed by ret ENDM output MACRO string,xtra ;; display string IFB <string> .ERR <missing operand in OUTPUT> EXITM ENDIF IFNB <xtra> .ERR <extra operand(s) in OUTPUT> EXITM ENDIF push eax ;; save EAX lea eax,string ;; string address push eax ;; string parameter on stack call outproc ;; call outproc(string) pop eax ;; restore EAX ENDM input MACRO dest,length,xtra ;; read string from keyboard IFB <length> .ERR <missing operand(s) in INPUT> EXITM ENDIF IFNB <xtra> .ERR <extra operand(s) in INPUT> EXITM ENDIF push ebx ;; save EBX lea ebx,dest ;; destination address push ebx ;; dest parameter on stack mov ebx,length ;; length of buffer push ebx ;; length parameter on stack call inproc ;; call inproc(dest,length) pop ebx ;; restore EBX ENDM .NOLISTMACRO ; suppress macro expansion listings .LIST ; begin listing
|
|
Most of the file IO.H consists of macro definitions that, when used, generate code to call external procedures. However, the file does contain other directives. It begins with a .NOLIST directive; this suppresses the listing of all source code, in particular the contents of IO.H. It then has EXTRN directives that identify the external procedures called by the macros. The file ends with a .NOLISTMACRO directive to suppress listing of any macro expansions and an .LIST directive so that the user 's statements following the directive INCLUDE io.h will again be shown in the listing file.
The bulk of the file IO.H consists of definitions for itoa , atoi , dtoa , atod , output , and input macros. These definitions have similar structures. Each uses IFB and IFNB directives to check that a macro call has the correct number of arguments. If not, .ERR directives are used to generate forced errors and appropriate messages. Actually, the checks are not quite complete.
Assuming that its arguments are correct, an input/output macro call expands to a sequence of instructions that call the appropriate external procedure, for instance itoaproc for the macro itoa . Parameters are passed on the stack, but some code sequences use a register to temporarily contain a value, with push and pop instructions to ensure that these registers are not changed following a macro call.
Exercises 9.5
|
|
Notice that itoa has only one error message that is used if either or both argument is missing. Rewrite the definition of itoa to provide complete argument checking. That is, check separately for missing source and dest arguments, generating specific messages for each missing argument. Allow for the possibility that both are missing.
|
|
| < Day Day Up > |
|
Introduction to 80x86 Assembly Language and Computer Architecture Authors: Detmer R. C. C. Published year: 2000 Pages: 65/113 |
![]() Introduction to Algorithms | ![]() Reversing: Secrets of Reverse Engineering | ![]() Professional Assembly Language (Programmer to Programmer) | ![]() Hacking: The Art of Exploitation, 2nd Edition |