Page 1 of 3

How do I print an integer that is interpreted as hex?

Posted: Thu Aug 17, 2017 5:36 am
by stevewoods1986
Hello.

I want to get a hex value (0x20 for example) and print it as a number (20). However, I'm having trouble.
I know I could just do

Code: Select all

cmp al, 0x20
je twentyprint

twentyprint:
mov ah, 0eh
mov al, 32h
int 10h

mov ah, 0eh
mov al, 30h
int 10h
however, that could use up a lot of space.
I want to print an integer so it shows a number instead of a symbol (like space is 0x20).

What should I do? Get the first nibble and interpret it as 2 and the second nibble as 0?
Knowing how to get a nibble from a register would be good.

Any help would be appreciated.

Thanks
Steve.

Re: How do I print an integer that is interpreted as hex?

Posted: Thu Aug 17, 2017 6:05 am
by iansjack
Read about the logical "and" and "or" instructions and the shift instructions.

Better yet, get a book on x64 assembler.

Re: How do I print an integer that is interpreted as hex?

Posted: Thu Aug 17, 2017 6:15 am
by MichaelFarthing
How long will everyone be fooled by this troll? Anyone who casually uses a word like nybble can't possibly be innocently making the sort of posts we see.

Re: How do I print an integer that is interpreted as hex?

Posted: Thu Aug 17, 2017 6:26 am
by iansjack
I'm still prepared to give him the benefit of the doubt. But the OP needs to accept that he is a raw beginner when it comes to assembler, let alone OS programming, rather than of "intermediate" ability. And the best way to remedy that is to do some serious studying rather than clogging up these forums with trivial questions.

More questions at this level and I'd be inclined to believe that he is a troll rather than someone seeking knowledge.

Re: How do I print an integer that is interpreted as hex?

Posted: Thu Aug 17, 2017 6:44 am
by BrightLight
stevewoods1986 wrote:What should I do? Get the first nibble and interpret it as 2 and the second nibble as 0?
Knowing how to get a nibble from a register would be good.
What you want is bit shifting, logical AND and arrays. They are irrelevant to OS development, so should be easy enough for you to figure out on your own.

Think of it this way: if this was in C and you had no itoa() or printf() function, how would you do it? Then after figuring that out, find the assembly instructions that can do that.

Re: How do I print an integer that is interpreted as hex?

Posted: Thu Aug 17, 2017 7:21 am
by Octacone
You need two char arrays, one to hold the output and one for the lookup table. Your lookup table needs to contain 16 chars, 0-9 to A-F.
Now what you need to do is: convert every digit to char, put everything inside your output array, print that array.
Take a look at these instructions for how to do it:

Code: Select all

and
shl
shr
Here is an useful clue:
1.ANDing 0xAB by 0x0F will give you B.
2.ANDing 0xAB by 0xF0 will give you A0.

Re: How do I print an integer that is interpreted as hex?

Posted: Thu Aug 17, 2017 8:35 am
by iansjack
You really don't need lookup tables.

Re: How do I print an integer that is interpreted as hex?

Posted: Thu Aug 17, 2017 9:05 am
by Octacone
iansjack wrote:You really don't need lookup tables.
True, you could even do it by modular base division and using ('0' + number) for conversion.

Re: How do I print an integer that is interpreted as hex?

Posted: Thu Aug 17, 2017 10:19 am
by stevewoods1986
Octacone wrote:
iansjack wrote:You really don't need lookup tables.
True, you could even do it by modular base division and using ('0' + number) for conversion.
What is a way of doing it? To be quite honest, I'm not the most experienced with instructions like AND and OR.

Re: How do I print an integer that is interpreted as hex?

Posted: Thu Aug 17, 2017 10:58 am
by obiwac
I'm not the most experienced with instructions like AND and OR.
Ok hes a troll.

Re: How do I print an integer that is interpreted as hex?

Posted: Thu Aug 17, 2017 11:16 am
by iansjack
stevewoods1986 wrote:To be quite honest, I'm not the most experienced with instructions like AND and OR.
Then, for goodness sake, do us all a favour. Go and learn a little about computer, programming, data structures, algorithms, etc., rather than wasting our time.

Re: How do I print an integer that is interpreted as hex?

Posted: Thu Aug 17, 2017 11:40 am
by Geri

Re: How do I print an integer that is interpreted as hex?

Posted: Thu Aug 17, 2017 12:38 pm
by stevewoods1986
iansjack wrote:
stevewoods1986 wrote:To be quite honest, I'm not the most experienced with instructions like AND and OR.
Then, for goodness sake, do us all a favour. Go and learn a little about computer, programming, data structures, algorithms, etc., rather than wasting our time.
I say I'm not the most experienced. I am not the expert on it. I understand about bitwise logic, OK? I might know more if I could print an integer.
Geri wrote:https://www.youtube.com/watch?v=jIo-Ea6GO64
I don't need this (BLEEP), ok? Understand? I'm already (BLEEP) (BLEEP)...ed off by everyone. Why do I have to be suggested things? I'm not wasting your time. Don't give me animated witch videos that looks like something from Pokemon. I actually saw a Pokemon advert while writing this :)

I can't believe you would link me to such (BLEEP). What do you think that video was going to do? Was it going to change anything or was it going to make this thread infected?

It's not my fault why most of you cause problems. Everyone here is a know-it-all. Ever head of Newton's Law?.

Sorry, about my swearing even if I bleeped it.

I want an answer, not complaining because you don't know the answer. If you don't know the answer, don't post.
I don't want trouble. I want an answer.

Why does every single forum thread get infected by people who complain.

Re: How do I print an integer that is interpreted as hex?

Posted: Thu Aug 17, 2017 1:00 pm
by neon
Hello,

You asked What is a way of doing it? after having already been given two different ways of doing it. No follow up questions from those responses. No attempt to figure out the algorithms they gave you. It is as if you completely ignored them thinking those responses were irrelevant.

You said you don't want suggestions. In the previous thread you said you don't want code or sample code. So: what do you want? Pseudo code? Links? A walkthrough tutorial? What can we do to provide you with what you want since apparently the two replies that provided correct solutions was not satisfactory?

Re: How do I print an integer that is interpreted as hex?

Posted: Thu Aug 17, 2017 1:03 pm
by stevewoods1986
neon wrote:Hello,

You asked What is a way of doing it? after having already been given two different ways of doing it. No follow up questions from those responses. No attempt to figure out the algorithms they gave you. It is as if you completely ignored them thinking those responses were irrelevant.

You said you don't want suggestions. In the previous thread you said you don't want code or sample code. So: what do you want? Pseudo code? Links? A walkthrough tutorial? What can we do to provide you with what you want since apparently the two replies that provided correct solutions was not satisfactory?
I don't want a puzzle. Fine, I'll figure it out.