Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Display Hexadecimal value stored at AH

How do i display hex value stored at AH?

Comments

  • jonathanebankjonathanebank Posts: 3Member
    Hope you can find out
  • valvesvalves Posts: 1Member
    You must divide it in 2 nybbles (4-bit) and then process each nybble.

    Try this:
    ------------------------------------------------------------------------

    ; Convert AH to Hex

    section .bss
    Buffer: resb 1 ; Text buffer - 1 Byte

    section .data ; Section containing initialized data
    HexStr: db "00", 10 ; 10 is EOL
    Digits: db "0123456789ABCDEF" ; Hexadecimal digits

    section .text

    global _start
    _start:

    ; Read a character from stdin:
    Read:
    mov eax, 3 ; sys_read call
    mov ebx, 0 ; File Descriptor 0: Standard Input
    mov ecx, Buffer ; Pass offset of the buffer to read to
    mov edx, 1 ; 1 byte to read
    int 80h ; Call sys_read

    cmp eax, 0 ; If eax=0, abort
    je Done ; Jump if equal (to 0, from compare)

    xor eax, eax ; Set eax to 0

    ; Get character from the buffer and put it in both eax and ebx:
    mov al, [Buffer] ; Put the byte from the input buffer into al
    mov ebx, eax ; Duplicate the byte in bl for second nybble

    ; Look up low nybble character and insert it into the string:
    and al, 0Fh ; Mask out all but the low nybble
    mov al, byte [Digits+eax] ; Look up the char equivalent of nyble
    mov byte [HexStr+1], al ; Write LSB char digit to line string

    ; Look up high nybble character and insert it into the string:
    shr bl, 4 ; Shift right high 4 bits of char into low 4 bits
    mov bl, byte [Digits+ebx] ; Look up char equivalent of nybble
    mov byte [HexStr], bl ; Write MSB char digit to line string

    ; Write the hexadecimal value to stdout:
    mov eax, 4 ; sys_write call
    mov ebx, 1 ; File Descriptor 1: Standard output
    mov ecx, HexStr ; Pass offset of line string
    mov edx, 3 ; Pass size of the line string
    int 80h ; Make kernel call to display line string

    Done:
    mov eax, 1 ; Exit Syscall
    mov ebx, 0 ; Status Code is zero
    int 80h ; kernel call

    ---------------------------------------------------------------------------
    This example was adapted and heavily modified from the book "Assembly Language Step-by-Step - Programming with Linux, 3rd Edition" - Jeff Duntemann / Published by Wiley Publishing, Inc., Indianapolis, Indiana / ISBN: 978-0-470-49702-9

    I hope it helps! - Vasco

Sign In or Register to comment.