Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Output AL to display?

radarfxstradarfxst Member Posts: 20
Hi,

I've been pulling my hair out for a few days now, and it's time for some professional help. I have to write a short assembly language program that takes one input from the keyboard (any single key), and displays its ascii hex code on the screen. For example, you run the program and it waits for an input from they keyboard. If you type a 0 (zero), it should display 30 on the screen (30 being the ascii hex code for zero). I've come up with several ways to echo the key on the screen, which would be great if that's what I needed to do.

Here's the shortest version of my many incorrect versions:

[code]
code segment
setdisplay:
;set disp mode 02h, erase all pages:
mov ah,00h
mov al,02h
int 10h
;set page 1:
mov ah,05h
mov al,01h
int10h

getkey:
mov ah,00h
int 16h

printkey:
;position the cursor:
mov ah,02h
mov bh,01h
mov dh,0ch
mov dl,
int 10h
;display the key
mov ah,02h
mov dl,al
int 21h
jmp getkey

code ends
[/code]

I also have versions with int 10h instead of 21h, but I get the same results.

I think it has something to do with moving the hex code from AL to memory, then extracting it as a string and printing the two characters one at a time - but I'm really not sure.

Any ideas?

Radarfxst

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [b][red]This message was edited by AsmGuru62 at 2005-12-14 6:16:22[/red][/b][hr]
    [blue]If you search this board for answers - you may find some code, but I will still explain the details of that conversion.

    Also, use functions, so you do not have to repeat blocks of code. Starting from basics now...

    Register AL has two HEX digits:
    - high 4 bits is a first digit
    - low 4 bits is a second digit

    [code]
    AL = 0110 0100 (HEX: 64h)
    [/code]

    Now, when you are converting to HEX, there are few ways to do it. The fastest way, is to use the code piece from 'Assembly Gems' - if you search the web - you should find it or got to my site - it has a link to it.

    However, you can also do it by indexing an array. Imagine the array where you put all HEX (ascii) characters as they ordered:

    [code]
    strHexArray db '0123456789ABCDEF'
    [/code]

    Now, if you take any 4 bits from AL and use them as a simple index into that array - you get corresponding digit. Here is a small procedure to do that:

    [code]
    Convert_BX_into_HexDL:
    ; ^^^ BX will contain the index and DL will return the digit
    mov si, offset [strHexArray] ; Get an address of the array
    mov dl, [bx+si] ; use the indexing mode to get data
    ret
    [/code]

    And now, using that procedure
    you can convert the whole AL into string:

    [code]
    strHexToDisplay db 'xx$'

    ...

    ConvertAL2HEX:
    xor bx, bx ; BX=0

    mov bl, al
    shr bx, 4 ; First 4 bits from AL is in BX now
    call Convert_BX_into_HexDL ; Convert (DL will contain a digit)
    mov [strHexToDisplay], dl ; save it for output

    mov bl, al
    and bx, 000Fh ; Last 4 bits from AL is in BX now
    call Convert_BX_into_HexDL ; Convert (DL will contain a digit)
    mov [strHexToDisplay+1], dl ; save it for output

    ret

    ; Now output few codes:

    mov al, 46
    call ConvertAL2HEX
    mov dx, offset [strHexToDisplay]
    mov ah, 9
    int 21h

    mov al, 0A7h
    call ConvertAL2HEX
    mov dx, offset [strHexToDisplay]
    mov ah, 9
    int 21h
    [/code]

    [/blue]


  • shaolin007shaolin007 Member Posts: 1,018
    [b][red]This message was edited by shaolin007 at 2005-12-15 13:14:36[/red][/b][hr]
    nevermind

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : :
    : [green]
    : For values under 100 you can use one 'div' instruction to do it.
    :
    : [code]
    : xor ax, ax ;zero ax
    : mov byte al, [your_number] ;load al with the number
    : mov dl, 10 ;divisor=10
    : div dl ;divide al/dl
    : add al, 30h ;this is your quotient+30h=ASCII
    : add ah, 30h ;this remainder+30h=ASCII
    : [/code]
    : For values greater than that see one of my previous posts.
    : [/green]
    :
    [blue]That will give decimal values, if I am not mistaken. HEX format was needed.[/blue]
  • shaolin007shaolin007 Member Posts: 1,018
    : : :
    : : [green]
    : : For values under 100 you can use one 'div' instruction to do it.
    : :
    : : [code]
    : : xor ax, ax ;zero ax
    : : mov byte al, [your_number] ;load al with the number
    : : mov dl, 10 ;divisor=10
    : : div dl ;divide al/dl
    : : add al, 30h ;this is your quotient+30h=ASCII
    : : add ah, 30h ;this remainder+30h=ASCII
    : : [/code]
    : : For values greater than that see one of my previous posts.
    : : [/green]
    : :
    : [blue]That will give decimal values, if I am not mistaken. HEX format was needed.[/blue]
    :
    [green]
    Yep your right, my bad.
    [/green]
  • IDKIDK Member Posts: 1,784

    : Any ideas?
    :
    This will do:
    [code]
    printHexInDh:
    push ax
    push dx

    mov dl,dh
    and dl,0F0h ;take the digit
    shr dl,4
    add dl,30h ;make ascii
    mov ah,2 ;print char
    int 21h

    mov dl,dh
    and dl,0Fh ;take the digit
    add dl,30h ;make ascii
    mov ah,2 ;print char
    int 21h

    push dx
    push ax
    ret
    [/code]

    Took me 5 min to write.

    Happy coding!

    [grey]The one and only [b]Niklas Ulvinge[/b][/grey] [white]aka [b]IDK[/b][/white]

  • shaolin007shaolin007 Member Posts: 1,018
    :
    : : Any ideas?
    : :
    : This will do:
    : [code]
    : printHexInDh:
    : push ax
    : push dx
    :
    : mov dl,dh
    : and dl,0F0h ;take the digit
    : shr dl,4
    : add dl,30h ;make ascii
    : mov ah,2 ;print char
    : int 21h
    :
    : mov dl,dh
    : and dl,0Fh ;take the digit
    : add dl,30h ;make ascii
    : mov ah,2 ;print char
    : int 21h
    :
    : [red]pop[/red] dx
    : [red]pop[/red] ax
    : ret
    : [/code]
    :
    : Took me 5 min to write.
    :
    : Happy coding!
    :
    : [grey]The one and only [b]Niklas Ulvinge[/b][/grey] [white]aka [b]IDK[/b][/white]
    :
    :
    [green]
    Just something I saw right off bat.
    [/green]

  • IDKIDK Member Posts: 1,784
    : :
    : : : Any ideas?
    : : :
    : : This will do:
    : : [code]
    : : printHexInDh:
    : : push ax
    : : push dx
    : :
    : : mov dl,dh
    : : and dl,0F0h ;take the digit
    : : shr dl,4
    : : add dl,30h ;make ascii
    : : mov ah,2 ;print char
    : : int 21h
    : :
    : : mov dl,dh
    : : and dl,0Fh ;take the digit
    : : add dl,30h ;make ascii
    : : mov ah,2 ;print char
    : : int 21h
    : :
    : : [red]pop[/red] dx
    : : [red]pop[/red] ax
    : : ret
    : : [/code]
    : :
    : : Took me 5 min to write.
    : :
    : : Happy coding!
    : :
    : : [grey]The one and only [b]Niklas Ulvinge[/b][/grey] [white]aka [b]IDK[/b][/white]
    : :
    : :
    : [green]
    : Just something I saw right off bat.
    : [/green]
    :
    Thanks, I did maybe write this a little to fast.
  • m34tb34tm34tb34t Member Posts: 25
    I know the thread is really old, but here's my two cents...

    I compile with FASM but should be easy to add your "MODEL TINY" statments etc... to make it compile for TASM

    [code]
    ; prints the scan code and ascii value of key strokes in hexadecimal
    ; high byte is scan code and low byte is ascii value
    ; these are the values returned by bios interrupt 16h
    ; escape key exits the program

    org 100h

    start_of_code:
    mov ah,9
    mov dx,infoMsg
    int 21h
    mov bp,4 ; change from 4 to 2 to only print ascii values
    loopMore: ; and not include the scan codes
    xor ax,ax
    int 16h ; get key stroke in ax
    mov cx,bp
    prLoop1:
    mov dx,ax ; save high nibble of AL from destruction
    and al,0Fh ; use only low nibble
    cmp al,10 ; if al < 10 set carry
    sbb al,69h ; 0-9: 96h..9Fh, A-F: A1h..A6h
    das ; 0-9 - 66h: 30h..39h, A-F - 60h: 41h..46h
    push ax ; save digit to stack
    mov ax,dx ; restore high nibble
    ror ax,4 ; shift next nibble into place
    loop prLoop1
    mov cx,bp
    prLoop2:
    pop ax
    int 29h
    loop prLoop2
    mov al,' '
    int 29h ; seperater for easier reading
    cmp ah,1 ; check escape key scan code
    jne loopMore ; loop until escape hit
    ret ; exit program
    infoMsg: db 'Escape key exits',13,10,'$'

    [/code]
  • m34tb34tm34tb34t Member Posts: 25
    And as a callable function...
    [code]
    ;output AL to display (as hexadecimal number)
    printHex:
    aam 16 ; db D4h, 10h if your assembler doen't know this opcode...
    cmp al,0Ah
    sbb al,69h ; "borrowed" this gem from someone named Norbert Juffa
    das
    xchg al,ah
    cmp al,0Ah
    sbb al,69h
    das
    int 29h
    mov al,ah
    int 29h
    ret
    [/code]
Sign In or Register to comment.