CIS-77 Home http://www.c-jump.com/CIS77/CIS77syllabus.htm

Lab M08. Console Input/Output


  1. Lab Description
  2. Data Input/Output
  3. IO.H Macros
  4. M08.ASM
  5. The output Macro (print ASCII characters)
  6. The input Macro (read ASCII characters)
  7. The szlen Macro (string length)
  8. The dtoa Macro (DWORD to ASCII)
  9. The dtoa Results
  10. The atod Macro (ASCII to DWORD)
  11. The atod Result
  12. The atod Scan
  13. The atoi and itoa Macros
  14. The atoi and itoa Example
  15. ATOI_ITOA_DEMO.ASM
  16. Lab Assignment

1. Lab Description



2. Data Input/Output



3. IO.H Macros




; CIS-77
; M08.ASM
; Demo program of IO.H and IO.ASM usage

.386                ; Tells MASM to use Intel 80386 instruction set.
.MODEL FLAT         ; Flat memory model
option casemap:none ; Treat labels as case-sensitive

INCLUDE IO.H        ; header file for input/output

.CONST          ; Constant data segment
    TXTPROMPT   BYTE    "Please enter up to 10 characters of text: ", 0
    TXTENTERED  BYTE    "                          You've entered: ", 0
    TXTLENGTH   BYTE    "            Number of characters entered: ", 0
    INPROMPT    BYTE    "                 Please enter an integer: ", 0
    ENDL        BYTE    ";", 13, 10, 0
    OVERFLOW    BYTE    "*** Bad number, please try again!", 0

.STACK 100h     ; (default is 1-kilobyte stack)

.DATA           ; Begin initialized data segment
    buffer      BYTE    12 DUP (?)
    dtoa_buffer BYTE    11 DUP (?)
    atoa_buffer BYTE    6  DUP (?)

.CODE           ; Begin code segment
_main PROC      ; Beginning of code

    output  TXTPROMPT        ; Please enter up to 10 characters of text...
    input   buffer, 12       ; ...read zero to 10 ASCII characters
    output  TXTENTERED       ; ...You've entered...
    output  buffer           ; ...print the input
    output  ENDL             ; new line
    
    szlen   buffer           ; Calculate length of user input, put result in eax
    dtoa    dtoa_buffer, eax ; convert 32-bit signed integer to string
    output  TXTLENGTH        ; Number of characters entered...
    output  dtoa_buffer      ; ...print numeric result
    output  ENDL             ; new line

    itoa    atoa_buffer, ax  ; Convert 16-bit signed integer to string
    output  TXTLENGTH        ; Number of characters entered...
    output  atoa_buffer      ; ...print numeric result
    output  ENDL             ; new line

@@:
    output  INPROMPT         ; Please enter an integer...
    input   buffer, 12       ; ...read zero to 10 ASCII characters
    atoi    buffer           ; convert "[+/-]123" to 2's complement (result in AX)
    jno     @F               ; check overflow flag
    output  OVERFLOW         ; print error message
    output  ENDL             ; new line
    jmp     @B               ; back to the prompt
@@:
    itoa    atoa_buffer, ax  ; Convert 16-bit signed integer to string
    output  TXTENTERED       ; ...You've entered...
    output  atoa_buffer      ; print result
    output  ENDL             ; new line
 
    ret
    
_main ENDP
END _main        ; Marks the end of the module and sets the program entry point label

4. The output Macro (print ASCII characters)



5. The input Macro (read ASCII characters)


6. The szlen Macro (string length)



7. The dtoa Macro (DWORD to ASCII)



8. The dtoa Results


9. The atod Macro (ASCII to DWORD)


10. The atod Result


11. The atod Scan



12. The atoi and itoa Macros



13. The atoi and itoa Example




; CIS-77
; ATOI_ITOA_DEMO.ASM
; ATOI and ITOA usage demo program

.386                ; Tells MASM to use Intel 80386 instruction set.
.MODEL FLAT         ; Flat memory model
option casemap:none ; Treat labels as case-sensitive

INCLUDE IO.H        ; header file for input/output

.CONST          ; Constant data segment
    TXTPROMPT   BYTE    "Please enter a number between -32768 and +32767: ", 0
    TXTENTERED  BYTE    "You've entered: ", 0
    ENDL        BYTE    13, 10, 0
    OVERFLOW    BYTE    "*** Bad number, please try again!", 0

.STACK 100h     ; (default is 1-kilobyte stack)

.DATA           ; Begin initialized data segment
    buffer      BYTE    8 DUP (?)

.CODE           ; Begin code segment
_main PROC      ; Beginning of code

@@:
    output  TXTPROMPT        ; Please enter a number...
    input   buffer, 8        ; Read zero to 6 ASCII characters
    atoi    buffer           ; Convert string to 2's complement number
    jno     @F               ; Check the overflow flag
    ; Handle input error:
    output  OVERFLOW         ; print error message
    output  ENDL             ; print new line
    jmp     @B               ; back to the prompt
@@:
    ; Success: result of conversion is in AX
    itoa    buffer, ax       ; Convert 16-bit signed integer to string
    output  TXTENTERED       ; You've entered...
    output  buffer           ; print result
    output  ENDL             ; print new line
 
    ret
    
_main ENDP
END _main        ; Marks the end of the module and sets the program entry point label

14. Lab Assignment