hex to ASCII translation

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Vakus
Posts: 7
Joined: Tue Jan 05, 2016 6:59 am
Libera.chat IRC: vakus

hex to ASCII translation

Post by Vakus »

I was updating my bootloader MicroKernel which is running 16 bit real mode

I modified code that suppose to translate hex decimal values to ASCII characters. My first version was working perfectly without any problems, however my newer function have some problems.

This is my new function which translates hex to ASCII values:

Code: Select all

;carry flag cleared if successed, if not then it is set
;input number should be in AL
;output number should be in AH
.translate:
    xor AH,AH
    mov BX, HEXASCII
    .loop:
        mov DL,[BX]
        cmp DL,AL
        je .end
        inc BX
        inc AH
        cmp AH,0x10
        je .err
        jmp .loop
    .end:
        clc
        ret
    .err:
        stc
        ret
; ... some code
HEXASCII db '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'

however this function seems to not work properly - when sending output from this function to int 13, bochs is returning error - read/write/verify parameter out of range. the numbers I use was 00 for Head, 00 for cylinder, and 01 for sector, so I guess that it is not an actual problem with arguments which I wrote.

I also used my previous function with the same arguments and it properly loaded sector I wanted to.

I tried to use Bochs debugger to track registers in memory before int 0x13, however it was looking like the registers have the same values I inputted.

My previous function is:

Code: Select all

;carry flag cleared if successed, if not then it is set
;input number should be in AL
;output number should be in AH
.translate:
    cmp AL,'0'
    je .x0
    cmp AL,'1'
    je .x1
    cmp AL,'2'
    je .x2
    cmp AL,'3'
    je .x3
    cmp AL,'4'
    je .x4
    cmp AL,'5'
    je .x5
    cmp AL,'6'
    je .x6
    cmp AL,'7'
    je .x7
    cmp AL,'8'
    je .x8
    cmp AL,'9'
    je .x9
    cmp AL,'a'
    je .xA
    cmp AL,'b'
    je .xB
    cmp AL,'c'
    je .xC
    cmp AL,'d'
    je .xD
    cmp AL,'e'
    je .xE
    cmp AL,'f'
    je .xF
    cmp AL,'A'
    je .xA
    cmp AL,'B'
    je .xB
    cmp AL,'C'
    je .xC
    cmp AL,'D'
    je .xD
    cmp AL,'E'
    je .xE
    cmp AL,'F'
    je .xF
    jmp .NONE
    .x0:
        xor AH,AH
        clc
        ret
    .x1:
        mov AH,0x1
        clc
        ret
    .x2:
        mov AH,0x2
        clc
        ret
    .x3:
        mov AH,0x3
        clc
        ret
    .x4:
        mov AH,0x4
        clc
        ret
    .x5:
        mov AH,0x5
        clc
        ret
    .x6:
        mov AH,0x6
        clc
        ret
    .x7:
        mov AH,0x7
        clc
        ret
    .x8:
        mov AH,0x8
        clc
        ret
    .x9:
        mov AH,0x9
        clc
        ret
    .xA:
        mov AH,0xA
        clc
        ret
    .xB:
        mov AH,0xB
        clc
        ret
    .xC:
        mov AH,0xC
        clc
        ret
    .xD:
        mov AH,0xD
        clc
        ret
    .xE:
        mov AH,0xE
        clc
        ret
    .xF:
        mov AH,0xF
        clc
        ret
    .NONE:
        xor AH,AH
        stc
        ret

I don't expect any other part of code to be damaged, as I did not modified it. If it is needed I will include the full code, however it is pretty long.

Is there anything which is missing or wrong in the new function?

NOTE: In this code I do not need to translate upper 4 bits of AL.

(NOTE: I also asked this question on stackoverflow: http://stackoverflow.com/questions/3711 ... ascii-char however it was not answered, so I decided to ask it here)

Also I am open for better way on how to translate hex to ASCII, however my understanding of assembly language as well as English language is limited, so I would be very thankful for detailed explanation of own way to translate this code.
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: hex to ASCII translation

Post by Schol-R-LEA »

vakus wrote:I modified code that suppose to translate hex decimal values to ASCII characters.
This statement is a non sequitur; a value in hex is already going to be in a character encoding. Hex is a representation of a number as a series of characters from 0...9 and A...F, and would likely be in a string like any other series of characters. The same is true of decimal, binary, octal, or any other representation of a number using conventional place-value numera, ls.

I assume that you meant to say that it converts a value in memory, which it interprets as an unsigned integer, into a string representation such as decimal or hexadecimal.

OK, why am I being so pedantic? Because precise expression is the name of the game in programming. You need to express the ideas clearly, and more importantly, you have to understand them clearly. The way you wrote that indicates to me that you may have a mistaken understanding of what the character encodings are, what the integer encodings are, and how the CPU represents data in general. I don't know if that's actually the case or not, but I would recommend that you think the problem out in detail before proceeding.

Note that I do not mean this as criticism; these are difficult ideas for anyone, and we have all had to learn the same lessons the hard way. Even experienced developers make these mistakes, despite knowing better, because programming is not something the human brain is well suited for.
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
Vakus
Posts: 7
Joined: Tue Jan 05, 2016 6:59 am
Libera.chat IRC: vakus

Re: hex to ASCII translation

Post by Vakus »

I wanted to make ASCII representation of hexadecimal value which is unsigned integer

I am sorry but English is not my native language - I started to learning it about 2 years ago, and I still have problems with clear explaining what do I mean sometimes
User avatar
BrightLight
Member
Member
Posts: 901
Joined: Sat Dec 27, 2014 9:11 am
Location: Maadi, Cairo, Egypt
Contact:

Re: hex to ASCII translation

Post by BrightLight »

Here you go. :roll:

Code: Select all

hex_values			db "0123456789ABCDEF" 
  
 ; hex_nibble_to_string: 
 ; Converts a nibble to a hex string 
 ; In\	Lower bits of AL = Nibble value 
 ; Out\	RSI = Pointer to string 
  
 hex_nibble_to_string: 
 	and rax, 0xF			; keep only the nibble 
 	add rax, hex_values 
 	mov dl, [rax] 
 	mov [.string], dl 
  
 	mov rsi, .string 
 	ret 
  
 .string:			times 2 db 0 
  
 ; hex_byte_to_string: 
 ; Converts a byte to a hex string 
 ; In\	AL = Byte 
 ; Out\	RSI = Pointer to string 
  
 hex_byte_to_string: 
 	mov [.byte], al 
 	movzx rax, al 
 	shr rax, 4			; high nibble 
 	call hex_nibble_to_string 
  
 	mov rdi, .string 
 	movsb 
  
 	movzx rax, [.byte] 
 	and rax, 0xF			; low nibble 
 	call hex_nibble_to_string 
  
 	mov rdi, .string+1 
 	movsb 
  
 	mov rsi, .string 
 	ret 
  
 .byte				db 0 
 .string:			times 3 db 0 
  
 ; hex_word_to_string: 
 ; Converts a 16-bit word to a hex string 
 ; In\	AX = Word 
 ; Out\	RSI = Pointer to string 
  
 hex_word_to_string: 
 	mov [.word], ax 
 	movzx rax, ax 
 	shr rax, 8			; high byte 
 	call hex_byte_to_string 
  
 	mov rdi, .string 
 	movsb 
 	movsb 
  
 	movzx rax, [.word] 
 	and rax, 0xFF			; low byte 
 	call hex_byte_to_string 
  
 	mov rdi, .string+2 
 	movsb 
 	movsb 
  
 	mov rsi, .string 
 	ret 
  
  
 .word				dw 0 
 .string:			times 5 db 0 
You know your OS is advanced when you stop using the Intel programming guide as a reference.
User avatar
Kazinsal
Member
Member
Posts: 559
Joined: Wed Jul 13, 2011 7:38 pm
Libera.chat IRC: Kazinsal
Location: Vancouver
Contact:

Re: hex to ASCII translation

Post by Kazinsal »

The algorithms for stuff like this are pretty simple programming problems.

If you can't figure these out, you are not capable of operating systems development.

This is not intended to be insulting. Just truth and reality.
Octocontrabass
Member
Member
Posts: 5587
Joined: Mon Mar 25, 2013 7:01 pm

Re: hex to ASCII translation

Post by Octocontrabass »

Vakus wrote:

Code: Select all

        mov DL,[BX]
        cmp DL,AL
INT 0x13 uses DL to select the disk. Your code changes DL. Are you putting the correct value in DL before you use INT 0x13?

(Why not use "cmp [BX], AL"?)
Vakus
Posts: 7
Joined: Tue Jan 05, 2016 6:59 am
Libera.chat IRC: vakus

Re: hex to ASCII translation

Post by Vakus »

Octocontrabass wrote:
Vakus wrote:

Code: Select all

        mov DL,[BX]
        cmp DL,AL
INT 0x13 uses DL to select the disk. Your code changes DL. Are you putting the correct value in DL before you use INT 0x13?

(Why not use "cmp [BX], AL"?)
Hmmm I did not realised that... this could explain problems with the whole code

Also I realised that I sentenced the question wrong because it is not HEX to ASCII but ASCII to HEX, but to be honest I would totally agree with
Kazinsal wrote:The algorithms for stuff like this are pretty simple programming problems.

If you can't figure these out, you are not capable of operating systems development.

This is not intended to be insulting. Just truth and reality.
because I am aware I am not capable of operating system development - I dont know assembly much, and I dont know any of C/C++, however trying to create operating system using everything I can find on internet, and help of people on forums such like this one allow me to learn it... it is actually the most efficient way I learn programming. And remember that people keep learning - maybe they are not capable of doing something now, but if they try over and over again they will become capable of doing it.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: hex to ASCII translation

Post by Combuster »

Knowing how to reason about your own code is also a very welcome skill. Especially when it comes to assembly you might want to practice commenting every line to state what the intermediate results actually mean. For instance "BX now points to the start of the string" or "AL now contains the length of the string".
mov BX, HEXASCII
.loop:
mov DL,[BX]
Let's start with this as it already does something odd - what values are assigned to BX and DL here? Once you've got that written down, have a second look. Is the actual process what you wanted it to be?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
MDenham
Member
Member
Posts: 62
Joined: Sat Nov 10, 2012 1:16 pm

Re: hex to ASCII translation

Post by MDenham »

Looking at the original code, it's ASCII representation of digit -> the digit itself.

As such, there's a much simpler algorithm to use (note that this leaves the digit in AL instead of moving it to AH):

Code: Select all

cmp al, 48
jl error    ; someone's passing a character that's not valid
sub al, 48  ; '0' == 48
cmp al, 10  ; was it a digit?
jl finish   ; yes, done
cmp al, 17  ; was it a letter?
jl error    ; nope, error
sub al, 7   ; 'A' was == 65, first sub made it == 17
cmp al, 16  ; after 'F'?
jl finish   ; nope, done
cmp al, 42  ; did someone pass a lowercase letter?
jl error    ; nope, error
sub al, 32  ; drop it back (hopefully) into the correct range one last time
cmp al, 16
jl finish
error:
stc
ret
finish:
clc
ret
Post Reply