Props to @Octacone for staying this calm all along I could personally never do such a featOctacone wrote:Okay, here is an example on how to do it. This is not the only way, there are other ways of doing it.stevewoods1986 wrote:I don't know what I want. I need something that is not code, I don't want to use somebody else's code. I don't want anime links with witches. How to do it? Stop fighting... You are all mean to me... Okay give me code, OK? OK? I don't care just give me some code...whatever. I just want to learn it myself...
I can't provide you with any useful Assembly code (because I mostly code in C++) but here is a walk-through.If you don't understand what these logical ANDs and shifts do, read my first reply again.
- Create an array (this will be used as an output object), make it hold 3 characters. 1st being used for the first digit, 2nd being used for the second digit, 3rd being used as a null terminator. (so you don't display any junk) (make sure your print function supports '\0')
- Create a second array, 16 characters in size. Make it equal "0123456789ABCDEF" (characters used for displaying hexadecimal values)
- Grab your first array (the output one), make its 0th element equal second_array[(number AND with 0xF0) --->> all shifted 4 bytes to the right];
- Grab your first array again, make its 1st element equal second_array[AND number with 0x0F];
- Print that array as a string.
- Profit!?
If you can't code this in Assembly, then you don't know Assembly. Take a step back, learn it and come back.
How do I print an integer that is interpreted as hex?
- obiwac
- Member
- Posts: 149
- Joined: Fri Jan 27, 2017 12:15 pm
- Libera.chat IRC: obiwac
- Location: Belgium
Re: How do I print an integer that is interpreted as hex?
Re: How do I print an integer that is interpreted as hex?
Bit AND is a simple operation that takes two inputs and returns true if both input 1 and input 2 are true, otherwise returns false.
Bit OR is simple like that, it simply takes two inputs and returns true if input 1 or input 2 is true (or both input 1 and input 2 are true), otherwise returns false.
AND simply does that:
OR simply does that:
Both AND, OR instructions does the same thing for each bit of input:
If you AND AL, BL with AL = 00110010 and BL = 01010001, result (stored in AL) will be 00010000
---
Bit shift is easy also, it simply "shifts" bits to left or right.
If you SHR AL, 4 with AL = 00110110, result (stored in AL) will be 00000011
Step by step operation would like that:
00110110 -> 00011011 -> 00001101 -> 00000110 -> 00000011
If you SHL AL, 4 with AL = 00110110, result (stored in AL) will be 01100000
Step by step operation would like that:
00110110 -> 01101100 -> 11011000 -> 10110000 -> 01100000
---
With ANDs, shifts and a lookup table, a hex-to-string function can be implemented easily.
Or you can do required ANDs and shifts, then you can add result to ASCII value of '0'
Bit OR is simple like that, it simply takes two inputs and returns true if input 1 or input 2 is true (or both input 1 and input 2 are true), otherwise returns false.
AND simply does that:
Code: Select all
IN1 IN2 OUT
0 0 0
1 0 0
0 1 0
1 1 1
Code: Select all
IN1 IN2 OUT
0 0 0
1 0 1
0 1 1
1 1 1
If you AND AL, BL with AL = 00110010 and BL = 01010001, result (stored in AL) will be 00010000
---
Bit shift is easy also, it simply "shifts" bits to left or right.
If you SHR AL, 4 with AL = 00110110, result (stored in AL) will be 00000011
Step by step operation would like that:
00110110 -> 00011011 -> 00001101 -> 00000110 -> 00000011
If you SHL AL, 4 with AL = 00110110, result (stored in AL) will be 01100000
Step by step operation would like that:
00110110 -> 01101100 -> 11011000 -> 10110000 -> 01100000
---
With ANDs, shifts and a lookup table, a hex-to-string function can be implemented easily.
Or you can do required ANDs and shifts, then you can add result to ASCII value of '0'
Keyboard not found!
Press F1 to run setup.
Press F2 to continue.
Press F1 to run setup.
Press F2 to continue.
- Schol-R-LEA
- Member
- Posts: 1925
- Joined: Fri Oct 27, 2006 9:42 am
- Location: Athens, GA, USA
Re: How do I print an integer that is interpreted as hex?
Note: I've been busy with this for a while, so I don't know what has been printed here since around 3PM or so.
I will post a general algorithm for converting between two bases later, but for this particular question, there is a shortcut based, as you noted, on the fact that a hex numeral exactly represent four bits. Given that, if we assume that we have a value less than 256 (one byte), then a basic algorithm might be written using Python as a pseudo-code, as such:
Just to explain a bit more (not sure if you need it, but again, a refresher can't hurt): when you AND two values, it leaves only the bits that are set in both values. For example,
This can be used to isolate specific bits or groups of bits, by having a 'mask' that matches the bits you want to capture. In this case, we want all of the low bits, and only those, so the mask we'd use for a one-byte value would be binary 00001111, or hex 0F.
The shift and rotate operations move the bits in the word either left (towards the most significant bit) or right (towards the least significant bit). With most instruction sets, 'shift' means that the bits that 'fall off the end' are discarded, while 'rotate' means the 'wrap around' the the other side. For example:
Some high-level languages such as C and Python support shifts, but not rotates; most CPUs do have both, as they are both trivial to implement in hardware.
Note that a shift is, in effect, the same as an integer multiply or divde by a power of 2:
40 == (5 SHL 3) == (5 * 2^3) == 40
31 == (255 SHL 3) == (255 // 2^3) == 31
If we can assume that we're using ASCII characters (or a compatible representation such as UTF-8), we can eliminate the numeral table, and instead just add each nibble with a value of 9 or less to the byte value 0x30, which is binary ASCII code for the numeral '0', while nibbles more than 9 can be subtract 10 (such that 0xA -> 0, 0xB => 1, etc.) and then add 0x41, the ASCII code for 'A'. Since the characters in question are sequential, this allows you to use the ASCII character set itself as an implicit table lookup.
I've written an example of how to implement this in x86 assembly which I tested by incorporating it into a stripped down version of my boot loader. However, it should be fairly straightforward to implement yourself.
I've attached the test files I wrote for each of these so you can confirm this; the Python code is for v 3.x, while the NASM code can be assembled with
I ran it with QEMU like so:
I also tested it with Bochs.
Whew, that's... a lot. I figured out some of the problems I was having with VERBUM along the way, though honestly I am not sure it is really something I need to be working on.
I will post a general algorithm for converting between two bases later, but for this particular question, there is a shortcut based, as you noted, on the fact that a hex numeral exactly represent four bits. Given that, if we assume that we have a value less than 256 (one byte), then a basic algorithm might be written using Python as a pseudo-code, as such:
Code: Select all
# look up table for the hexadecimal numerals
hex_numerals = "0123456789ABCDEF"
def byte2hex(value):
hex_str = ""
# to get the low bits, you apply a 'mask'
# to the high bits using a bitwise AND.
low_nibble = value & 0x0F
hex_str += hex_numerals[low_nibble]
# to get the high bits, shift the value to the right
# by four. The low bits will fall off of the byte,
# and the high bits will now be in the lower bits.
high_nibble = value >> 4
hex_str = hex_numerals[high_nibble] + hex_str
return hex_str
Code: Select all
00100011 AND 01000110 = 00000100
The shift and rotate operations move the bits in the word either left (towards the most significant bit) or right (towards the least significant bit). With most instruction sets, 'shift' means that the bits that 'fall off the end' are discarded, while 'rotate' means the 'wrap around' the the other side. For example:
Code: Select all
01101101 Shift left 2 == 10110100
01101101 Shift right 2 == 00011011
01101101 rotate left 2 == 10110001
01101101 rotate right 2 == 01011011
Note that a shift is, in effect, the same as an integer multiply or divde by a power of 2:
40 == (5 SHL 3) == (5 * 2^3) == 40
31 == (255 SHL 3) == (255 // 2^3) == 31
If we can assume that we're using ASCII characters (or a compatible representation such as UTF-8), we can eliminate the numeral table, and instead just add each nibble with a value of 9 or less to the byte value 0x30, which is binary ASCII code for the numeral '0', while nibbles more than 9 can be subtract 10 (such that 0xA -> 0, 0xB => 1, etc.) and then add 0x41, the ASCII code for 'A'. Since the characters in question are sequential, this allows you to use the ASCII character set itself as an implicit table lookup.
Code: Select all
def byte2hex(value):
# to get the low bits, you apply a 'mask'
# to the high bits using a bitwise AND.
low_nibble = value & 0x0F
# to get the high bits, shift the value to the right
# by four. The low bits will fall off of the byte,
# and the high bits will now be in the lower bits.
high_nibble = value >> 4
hex_str = ""
for nibble in [high_nibble, low_nibble]:
if nibble <= 9:
hex_str += chr(nibble + 0x30)
else:
hex_str += chr((nibble - 10) + 0x41)
return hex_str
I've written an example of how to implement this in x86 assembly which I tested by incorporating it into a stripped down version of my boot loader. However, it should be fairly straightforward to implement yourself.
Code: Select all
byte2hex:
push cx
push di
mov cx, 2
mov al, bl
mov ah, bl
and al, 0x0f ; isolate low nibble
shr ah, 4 ; isolate high nibble into low nibble
.nibble_loop:
cmp ah, 9
jg .alphanum
add ah, 0x30
jmp short .store_char
.alphanum:
add ah, (0x41 - 0xA) ; NASM will compute imm. value
.store_char:
mov [di], ah
mov ah, al
inc di
loop .nibble_loop
pop di
pop cx
ret
Code: Select all
nasm -f bin hexprint.asm -o hexprint.bin
Code: Select all
qemu-system-i386 hexprint.bin
Whew, that's... a lot. I figured out some of the problems I was having with VERBUM along the way, though honestly I am not sure it is really something I need to be working on.
- Attachments
-
- hexprint.zip
- (2.45 KiB) Downloaded 36 times
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.
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.
- Schol-R-LEA
- Member
- Posts: 1925
- Joined: Fri Oct 27, 2006 9:42 am
- Location: Athens, GA, USA
Re: How do I print an integer that is interpreted as hex?
I know, another sequential post, but... I just figured out the real reason for one of the problems I was having, one I thought I had figured out earlier today: I had the names of the standard sections wrong.
By one character. The leading period.
Somebody shoot me.
By one character. The leading period.
Somebody shoot me.
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.
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.
- Schol-R-LEA
- Member
- Posts: 1925
- Joined: Fri Oct 27, 2006 9:42 am
- Location: Athens, GA, USA
Re: How do I print an integer that is interpreted as hex?
Sorry to seqpost again... I thought this was a different thread for some reason. To the mods: should I merge these?
At this point, I doubt even Brendan recalls the circumstances of the first ban (OK, he probably does, but I'm not sure if anyone else other than some of the other mods ever knew), but after that stunt, there was no way they were letting him back on.
He has since been making some rather weaksauce attempts at 'DDOS'-ing the site by... well, it isn't really DDOS, because it is only on one or two machines AFAIK, but he creates more sock-puppets and runs scripts to automatically post messages saying "reinstate andrewthompson555" or words to those effect, usually with random obscenities and racism thrown in. This usually lasts exactly as long as it takes for one of the mods to log in, see it, and ban that account, too.
His goal appears to be to annoy the mods and users so much that they relent just to get him to stop - I guess he never read the classic fable of the bet between the Wind and the Sun.
I didn't see the instance of spamming you mentioned in the other thread about the forum, but my guess is that that was him again.
The main result of this is that it eats up a lot of the time the mods need for basic maintenance and routine moderation. That having been said, even as 'leet haxxors' (that is to say, script kiddies) go, he's strictly small potatoes - even at maximum volume, he barely comes close to the amount of junk most sites get in a given day just from web spiders advertising herbal Viagra or what have you.
the upshot of all of this is that whenever a new member starts acting in ways people find annoying, they accuse that person of being one more clever disguise of andrewthompson555.
Yes, but in this instance, Zaval was only quoting it, not starting it. That 'word' was the username for one of the many sock-puppet accounts created by @andrewthompson555, a user who got a temporary ban last Fall. Apparently, he decided to make it permanent by creating a second account (which is against the site's TOS) and then harassing the mods both in the forum and PM, demanding the temporary ban be lifted immediately and insisting that they were at fault.stevewoods1986 wrote:Racist, huh? Racism.Or maybe this is yet another "(BLEEP)" incarnation, taking another approach for doing bad things?
At this point, I doubt even Brendan recalls the circumstances of the first ban (OK, he probably does, but I'm not sure if anyone else other than some of the other mods ever knew), but after that stunt, there was no way they were letting him back on.
He has since been making some rather weaksauce attempts at 'DDOS'-ing the site by... well, it isn't really DDOS, because it is only on one or two machines AFAIK, but he creates more sock-puppets and runs scripts to automatically post messages saying "reinstate andrewthompson555" or words to those effect, usually with random obscenities and racism thrown in. This usually lasts exactly as long as it takes for one of the mods to log in, see it, and ban that account, too.
His goal appears to be to annoy the mods and users so much that they relent just to get him to stop - I guess he never read the classic fable of the bet between the Wind and the Sun.
I didn't see the instance of spamming you mentioned in the other thread about the forum, but my guess is that that was him again.
The main result of this is that it eats up a lot of the time the mods need for basic maintenance and routine moderation. That having been said, even as 'leet haxxors' (that is to say, script kiddies) go, he's strictly small potatoes - even at maximum volume, he barely comes close to the amount of junk most sites get in a given day just from web spiders advertising herbal Viagra or what have you.
the upshot of all of this is that whenever a new member starts acting in ways people find annoying, they accuse that person of being one more clever disguise of andrewthompson555.
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.
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.
-
- Member
- Posts: 80
- Joined: Wed Aug 09, 2017 7:37 am
Re: How do I print an integer that is interpreted as hex?
So it was a DoS attack. How did this happen? Why isn't he still doing it? By the way, I know Zaval was only quoting it but it is still offensive.Schol-R-LEA wrote:Sorry to seqpost again... I thought this was a different thread for some reason. To the mods: should I merge these?
Yes, but in this instance, Zaval was only quoting it, not starting it. That 'word' was the username for one of the many sock-puppet accounts created by @andrewthompson555, a user who got a temporary ban last Fall. Apparently, he decided to make it permanent by creating a second account (which is against the site's TOS) and then harassing the mods both in the forum and PM, demanding the temporary ban be lifted immediately and insisting that they were at fault.stevewoods1986 wrote:Racist, huh? Racism.Or maybe this is yet another "(BLEEP)" incarnation, taking another approach for doing bad things?
At this point, I doubt even Brendan recalls the circumstances of the first ban (OK, he probably does, but I'm not sure if anyone else other than some of the other mods ever knew), but after that stunt, there was no way they were letting him back on.
He has since been making some rather weaksauce attempts at 'DDOS'-ing the site by... well, it isn't really DDOS, because it is only on one or two machines AFAIK, but he creates more sock-puppets and runs scripts to automatically post messages saying "reinstate andrewthompson555" or words to those effect, usually with random obscenities and racism thrown in. This usually lasts exactly as long as it takes for one of the mods to log in, see it, and ban that account, too.
His goal appears to be to annoy the mods and users so much that they relent just to get him to stop - I guess he never read the classic fable of the bet between the Wind and the Sun.
I didn't see the instance of spamming you mentioned in the other thread about the forum, but my guess is that that was him again.
The main result of this is that it eats up a lot of the time the mods need for basic maintenance and routine moderation. That having been said, even as 'leet haxxors' (that is to say, script kiddies) go, he's strictly small potatoes - even at maximum volume, he barely comes close to the amount of junk most sites get in a given day just from web spiders advertising herbal Viagra or what have you.
the upshot of all of this is that whenever a new member starts acting in ways people find annoying, they accuse that person of being one more clever disguise of andrewthompson555.
- Schol-R-LEA
- Member
- Posts: 1925
- Joined: Fri Oct 27, 2006 9:42 am
- Location: Athens, GA, USA
Re: How do I print an integer that is interpreted as hex?
I don't know any more of the details than I mentioned; you might want to PM Brendan about it if you want to know more. As for why he stopped, well, AFAIK he hasn't - he just hasn't been able to get past the mods long enough for most others to see it except on rare occasions like the time last week.stevewoods1986 wrote:So it was a DoS attack. How did this happen? Why isn't he still doing it?
I can't disagree, it most certainly is.stevewoods1986 wrote:By the way, I know Zaval was only quoting it but it is still offensive.
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.
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.