flat assembler
Message board for the users of flat assembler.

flat assembler > Main > Printing binary in asm

Author
Thread Post new topic Reply to topic
moveax41h



Joined: 18 Feb 2018
Posts: 47
I apologize if this question seems really basic. I am new to "thinking in assembly" if you will. As I'm sure you all know, there is a little bit of a learning curve from going from high level language algorithms to asm thinking...

That said, what I'm doing is writing small programs in asm that help me better understand asm itself. I'm writing a small program that should print the FLAGS register. This is what I currently have:
Code:
section '.data' data readable writeable
flagsmsg db "FLAGS is: %.8X\n", 0
section '.code' code readable executable
...
pushf
push flagsmsg
call [printf]
    


I've excluded most of the program to be concise but you can see what I'm doing here.

This code successfully prints the FLAGS register to the console, but it does it in HEX... I want to print it in BINARY such that I can see every single bit. Eventually, I want to label each bit so I can see which flags are set. But I do not know how one would go about printing binary straight to the console... I know in C that you have to do a workaround using a switch-case because there's not a "binary format specifier." Is there a simple way to do this in asm? Thank you.

_________________
-moveax41h
Post 27 Feb 2018, 03:38
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 419
Location: Belarus
moveax41h wrote:
I know in C that you have to do a workaround using a switch-case because there's not a "binary format specifier." Is there a simple way to do this in asm? Thank you.

The question has nothing to do with C vs asm differences. You’re using a C function printf, so what would you expect? Either the function has such capability or you have to implement the feature yourself. Just like in any other programming language. Seems like this time you’re unlucky and have to write it yourself.
Post 27 Feb 2018, 07:03
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16054
Location: 112 Ocean Avenue, Amityville
moveax41h wrote:
... printing binary straight to the console...
By "print ... straight to the console" do you mean using the OS services? Or do you mean programming the hardware yourself? Or do you mean using the CRT library functions?

Each of these three would require different code to accomplish.

Do you already know how to convert a value into a string of 0's and 1's? I'm sure we can help if this is something you are not sure about.
Post 27 Feb 2018, 07:12
View user's profile Send private message Visit poster's website Reply with quote
yeohhs



Joined: 19 Jan 2004
Posts: 170
Location: N 5.43564° E 100.3091°
moveax41h wrote:

This code successfully prints the FLAGS register to the console, but it does it in HEX... I want to print it in BINARY such that I can see every single bit. Eventually, I want to label each bit so I can see which flags are set. But I do not know how one would go about printing binary straight to the console... I know in C that you have to do a workaround using a switch-case because there's not a "binary format specifier." Is there a simple way to do this in asm? Thank you.


There is an interesting discussion about this here.
https://stackoverflow.com/questions/40811218/creating-an-x86-assembler-program-that-converts-an-integer-to-a-16-bit-binary-st

_________________
“It’s not that we have a short time to live, but that we waste a lot of it.”
-Lucius Annaeus Seneca
Post 27 Feb 2018, 08:39
View user's profile Send private message Visit poster's website Reply with quote
moveax41h



Joined: 18 Feb 2018
Posts: 47
yeohhs thanks for the link, that's helpful.

revolution, that was a good question. I think I'm going to stick to msvcrt funcs for now. I think programming the hardware myself would be cool too but I have no knowledge of how to go about that other than by using 8086emu and interrupts. I don't think I can do that on a current Windows machine anymore right?
Post 28 Feb 2018, 01:31
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16054
Location: 112 Ocean Avenue, Amityville
So if you use the MSVCRT DLL functions then one way to do what you want is to convert the value to a string of 0s/1s, and then you can use printf to output the string with %s.
Post 28 Feb 2018, 10:39
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2309
Location: Usono (aka, USA)
There's only 16 total hex nibbles anyways. So just use a small lookup table. Honestly, hex is very easy to convert to binary (1:1), so you can do it in your head fairly easily. Decimal 8 is bit 3 is 1000, decimal 15 (0xF) of all four bits (8+4+2+1) is 1111, decimal 10 (0xA, 8+2) is 1010.
Post 28 Feb 2018, 21:38
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2018, Tomasz Grysztar.

Powered by rwasa.