home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / Assembly / VIDEO_13.ASM < prev    next >
Assembly Source File  |  1986-09-23  |  3KB  |  103 lines

  1. CODE_SEG    SEGMENT PUBLIC
  2.     ASSUME    CS:CODE_SEG
  3.  
  4.     PUBLIC    WRITE_HEX
  5. ;-----------------------------------------------------------------------;
  6. ; This procedure converts the byte in the DL register to hex and writes    ;
  7. ; the two hex digits at the current cursor position.            ;
  8. ;                                    ;
  9. ;    DL    Byte to be converted to hex.                ;
  10. ;                                    ;
  11. ; Uses:        WRITE_HEX_DIGIT                        ;
  12. ;-----------------------------------------------------------------------;
  13. WRITE_HEX    PROC    NEAR        ;Entry point
  14.     PUSH    CX            ;Save registers used in this procedure
  15.     PUSH    DX
  16.     MOV    DH,DL            ;Make a copy of byte
  17.     MOV    CX,4            ;Get the upper nibble in DL
  18.     SHR    DL,CL
  19.     CALL    WRITE_HEX_DIGIT        ;Display first hex digit
  20.     MOV    DL,DH            ;Get lower nibble into DL
  21.     AND    DL,0Fh            ;Remove the upper nibble
  22.     CALL    WRITE_HEX_DIGIT        ;Display second hex digit
  23.     POP    DX
  24.     POP    CX
  25.     RET
  26. WRITE_HEX    ENDP
  27.  
  28.     PUBLIC    WRITE_HEX_DIGIT
  29. ;-----------------------------------------------------------------------;
  30. ; This procedure converts the lower 4 bits of DL to a hex digit and    ;
  31. ; writes it to the screen.                        ;
  32. ;                                    ;
  33. ;    DL    Lower 4 bits contain number to be printed in hex.    ;
  34. ;                                    ;
  35. ; Uses:        WRITE_CHAR                        ;
  36. ;-----------------------------------------------------------------------;
  37. WRITE_HEX_DIGIT        PROC    NEAR
  38.     PUSH    DX            ;Save registers used
  39.     CMP    DL,10            ;Is this nibble <10?
  40.     JAE    HEX_LETTER        ;No, convert to a letter
  41.     ADD    DL,"0"            ;Yes, convert to a digit
  42.     JMP    Short WRITE_DIGIT    ;Now write this character
  43. HEX_LETTER:
  44.     ADD    DL,"A"-10        ;Convert to hex letter
  45. WRITE_DIGIT:
  46.     CALL    WRITE_CHAR        ;Display the letter on the screen
  47.     POP    DX            ;Restore old value of AX
  48.     RET
  49. WRITE_HEX_DIGIT        ENDP
  50.  
  51.     PUBLIC    WRITE_CHAR
  52. ;-----------------------------------------------------------------------;
  53. ; This procedure prints a character on the screen using the DOS        ;
  54. ; function call.                            ;
  55. ;                                    ;
  56. ;    DL    Byte to print on screen.                ;
  57. ;-----------------------------------------------------------------------;
  58. WRITE_CHAR    PROC    NEAR
  59.     PUSH    AX
  60.     MOV    AH,2            ;Call for character output
  61.     INT    21h            ;Output character in DL register
  62.     POP    AX            ;Restore old value in AX
  63.     RET                ;And return
  64. WRITE_CHAR    ENDP
  65.  
  66.     PUBLIC    WRITE_DECIMAL
  67. ;-----------------------------------------------------------------------;
  68. ; This procedure writes a 16-bit, unsigned number in decimal notation.    ;
  69. ;                                    ;
  70. ;    DX    N : 16-bit, unsigned number.                ;
  71. ;                                    ;
  72. ;-----------------------------------------------------------------------;
  73. WRITE_DECIMAL    PROC    NEAR
  74.     PUSH    AX            ;Save registers used here
  75.     PUSH    CX
  76.     PUSH    DX
  77.     PUSH    SI
  78.     MOV    AX,DX
  79.     MOV    SI,10            ;Will divide by 10 using SI
  80.     XOR    CX,CX            ;Count of digits placed on stack
  81. NON_ZERO:
  82.     XOR    DX,DX            ;Set upper word of N to 0
  83.     DIV    SI            ;Calculate N/10 and (N mod 10)
  84.     PUSH    DX            ;Push one digit onto the stack
  85.     INC    CX            ;One more digit added
  86.     OR    AX,AX            ;N = 0 yet?
  87.     JNE    NON_ZERO        ;Nope, continue
  88. WRITE_DIGIT_LOOP:
  89.     POP    DX            ;Get the digits in reverse order
  90.     CALL    WRITE_HEX_DIGIT
  91.     LOOP    WRITE_DIGIT_LOOP
  92. END_DECIMAL:
  93.     POP    SI
  94.     POP    DX
  95.     POP    CX
  96.     POP    AX
  97.     RET
  98. WRITE_DECIMAL    ENDP
  99.  
  100. CODE_SEG    ENDS
  101.  
  102.     END
  103.