Page 1 of 1

ASM print bytes, words, dwords, and qwords as dec,bin, hex

Posted: Tue Oct 27, 2009 10:49 am
by Goose007
i tried searching for this and haven't been successful. I want to print the decimal values that are in the registers or even in data to ‘int 10’. Signed and unsigned. Also a register/value to hex would be nice. I was able to use someone elses code to write the value as hex but it seemed very clunky and I ended up having to add a null terminator to the string. I will post what I have for the Hex. I AM USING FASM

Code: Select all

        mov word [reg16], ax            ;look at register
        push ax
        call printreg16
        pop ax

hang:
        jmp hang

printreg16:
        mov di, outstr16
        mov ax, [reg16]
        mov si, hextable
        mov cx, 4                       ;loop 4 times

        .hexloop:
                rol ax, 4               ;leftmost will
                mov bx, ax              ;become
                and bx, 0x0F            ;rightmost
                mov bl, [si+bx]         ;index into hexstr
                mov [di], bl

                inc di
                dec cx

                jnz .hexloop

                mov [outstr16+4], 0
                mov si, outstr16
                call print_string
 
        ret
print_string:
        lodsb           ; grab a byte from SI

        or al, al       ; logical or al by itself
        jz .done        ; if the result is 0x00 we are done

        mov ah, 0x0E
        int 0x10        ; print out the character in al

        jmp print_string

        .done:
                ret

outstr16   rb 5  ;register value string
reg16      dw 0x0000
hextable   db '0123456789ABCDEF'

Re: ASM print bytes, words, dwords, and qwords as dec,bin, hex

Posted: Tue Oct 27, 2009 11:45 am
by Combuster
Have you ever tried converting binary to any other form on paper? Have you ever tried teaching a computer program to do the same? What have you tried with what results? Did you know we don't spoonfeed code? Have you read the forum rules and all links going from there?

In other words, you just failed the required skill check for OS development.

Re: ASM print bytes, words, dwords, and qwords as dec,bin, hex

Posted: Tue Oct 27, 2009 3:16 pm
by Goose007
Yes i have done it on paper i have done it plenty of times in other languages. I am posting this in the general programming section because it doesn't have to do with OS development. I am trying to learn ASM. I'm sorry I'm not an elitist forum troll like you. However if you could provide me with these links as my searching skills seem to be fail, and then I would be greatly appreciative of your time. However in other languages converting the integer (word/doubleword) to a string representation of a decimal value is easily determined val(), itoa(,,)

As an update:
Also looking for nibble to Hex, dec, bin...

Re: ASM print bytes, words, dwords, and qwords as dec,bin, hex

Posted: Tue Oct 27, 2009 3:42 pm
by gravaera
Something I've been wondering of late is why people come here to learn ASM. This is not the place to learn a programming language. The very subject of this entire site should key visitors in to the prerequisites.

The General Programming section is not for your lethargic convenience. Please only come to an Operating System Development forum if your primary aim is to build an Operating System.

There are many general programming helper sites on the internet. Try going elsewhere for programming aid. When you come here, post OS questions.

Combuster is not being unreasonable. Please understand that the forum rules state that we do not babysit inexperienced programmers. Kindly have respect for our community and understand that a certain minimum level of expertise is required before joining. IIRc, there's even an entry quiz.

I wish people would have more respect for the purpose of these forums~.

Anyway, to offset the slightly harsh tone of this post, you may find useful links on learning ASM for x86, and probably other architectures in the Wiki. Please note also that you are required to read the Intel Manuals before posting up questions, as most of your questions will be answered in there.

In the wiki, on the main page, http://wiki.osdev.org , there is a small sectioned off area titled "Languages". Please read the article called "Assembly", and follow the links at the bottom for good learning resources.

-gravaera.

Re: ASM print bytes, words, dwords, and qwords as dec,bin, hex

Posted: Tue Oct 27, 2009 7:27 pm
by Goose007
gravaera: Thank you for your links. As for learning ASM, I do apologize for posting on your forums. I saw a plethora of ASM and figured I could get better help here then anywhere else; considering the amount of ASM that goes into an OS. As far as the answer to my original question. I was able to fiddle around and come up with at least part of my answer for now. I will post code once I have answered all my own questions. Aside from that I meet all other requirements for the site... and may be joining your community in the future... Hobby of course. I have many years of experience with C, C++, C#, and VB. Assembly has always intrigued me and figure it was about time I learn. Thanks again. Hope to see you all soon!

Re: ASM print bytes, words, dwords, and qwords as dec,bin, hex

Posted: Tue Oct 27, 2009 7:34 pm
by pcmattman
Can you implement an algorithm to convert from an integer to a string in C, without using itoa and friends?

If you can't, I'd suggest implementing it in C and then translating it to assembly. This means you get two bonuses: you learn how to convert C to Assembly, and you also get to learn the algorithm without having trouble because you're still learning the language.

If you can do it in C already, without using itoa or similar functions, then ignore me :)

Re: ASM print bytes, words, dwords, and qwords as dec,bin, hex

Posted: Tue Oct 27, 2009 8:54 pm
by stephenj
If you're a C programmer, you have the option of using gcc to output asm for you to examine...

Code: Select all

echo "int main(void) { return 0; }" > test.c
gcc test.c -o test.s -S
cat test.s
If you are on Windows, install cygwin to do this.

In the future, when you are starting at a forum, avoid using the term "elitist forum troll" when you are referring to a respected member of said forum. His message isn't wrong, you asked to be spoonfed (whether you realize it or not), which doesn't go over well here.

Welcome to the forum.

Re: ASM print bytes, words, dwords, and qwords as dec,bin, hex

Posted: Tue Oct 27, 2009 9:20 pm
by Goose007
stephenj: Thank you... been so long since I've used C (back when palm debuted in 1996 or somewhere there abouts) i forgot. As for my reply to combuster. Instead of being professional or cordial he/she chose to be a sarcastic @$$. I can only be as nice as I'm treated. I do appreciate all the help you(the community) have given me. As for the spoonfeeding I guess I worded my OP incorrectly. No worries. I am working/learning the code pretty quick. Just have to write helper functions that I'm use to having. I have the register to decimal down... except for the 32 bit (doublewords) but that might be a different issue.

Re: ASM print bytes, words, dwords, and qwords as dec,bin, hex

Posted: Tue Oct 27, 2009 10:39 pm
by gravaera
@OP: Oi! Big misconception: these forums are owned by chase, the founder. Apart from that, carry on. Sorry for my interjection.

-All the best
gravaera

Re: ASM print bytes, words, dwords, and qwords as dec,bin, hex

Posted: Wed Oct 28, 2009 12:33 am
by Combuster
Goose007 wrote:Instead of being professional or cordial he/she chose to be a sarcastic @$$.
Oh really? The first three questions regard proper asking style: what did you do, what not, and what (failed) results came from there. The fourth is a reminder (you apparently needed), and the fifth is a reference to where you can get more information/make better use of this place. If you want to interpret that as trolling, then that's up to you.
gravaera wrote:IIRc, there's even an entry quiz.
Officially, no. Not that I considered proposing one (you don't need to look, but it gives an indication of the level required)

Re: ASM print bytes, words, dwords, and qwords as dec,bin, hex

Posted: Wed Oct 28, 2009 12:58 am
by tantrikwizard
FASM is cool but has some limitations. Unless you're going to write the entire kernel in FASM I wouldn't recommend using it. It has a very powerful macro language but its also very complicated and not portable to other assemblers. Most assembler macro languages aren't portable but FASM particularly is highly proprietary. My 2 cents, use JWASM instead, in buries most other assemblers IMO.

Re: ASM print bytes, words, dwords, and qwords as dec,bin, hex

Posted: Wed Oct 28, 2009 1:10 am
by Andr3w
tantrikwizard wrote:FASM is cool but has some limitations. Unless you're going to write the entire kernel in FASM I wouldn't recommend using it. It has a very powerful macro language but its also very complicated and not portable to other assemblers. Most assembler macro languages aren't portable but FASM particularly is highly proprietary. My 2 cents, use JWASM instead, in buries most other assemblers IMO.
Well, I don't think that you shouldn't use FASM just because of portability of its macro language.
And some people just don't use macros. Me too :P

Re: ASM print bytes, words, dwords, and qwords as dec,bin, hex

Posted: Wed Oct 28, 2009 1:31 am
by tantrikwizard
qandrew wrote:Well, I don't think that you shouldn't use FASM just because of portability of its macro language.
And some people just don't use macros. Me too :P
There's other reasons not to use FASM beside macro portability. The last version I used only supported COFF, flat or MS executable formats. The COFF format was also incompatible with several linkers which linked COFF files from other compilers/assemblers without a problem. They claim ELF is supported in recent versions but I haven't tried it. However, I think portability of the macro language is important to some degree. Most generic macros can easily be ported to other assemblers...except for FASM...its highly proprietary whereas most other assemblers seem to follow a common paradigm. Don't expect to spend any time learning the FASM macro syntax and using it elsewhere, on the other hand, if you learn TASM, MASM, NASM, etc syntax, its rather easy jump around. If an assembler is going to act as a linker (which FASM does) IMO it should also link in external .a or .obj files but its support is very limited. Again, some .obj files built with other tools will not link properly in FASM. So either you write your entire kernel in FASM or mix/match and deal with a LOT of uneccessary issues in the process which could be solved by choosing a proper assembler to begin with. :)) You can hack/slash it to work with a limited set of other tools, but its just not worth it IMO when better tools are around.

Re: ASM print bytes, words, dwords, and qwords as dec,bin, hex

Posted: Wed Oct 28, 2009 4:55 am
by stephenj
Goose007 wrote:As for my reply to combuster. Instead of being professional or cordial he/she chose to be a sarcastic @$$. I can only be as nice as I'm treated. I do appreciate all the help you(the community) have given me.
Combuster didn't say anything wrong. You violated the forum's rules, he pointed it out. Notice what he said:
Combuster wrote:... you just failed the required skill check for OS development.
This forum gets a lot of people who want to make an OS, but aren't ready. This is how it is dealt with. If you don't like it, then you probably won't want to be on this forum.

By the way, I noticed that people who would generally be racing to help you, were silent on this thread.