November 16th, 2009, 09:38 PM
Number Conversion (Dec -> Bin and Dec -> Hex)
I get the algorithm of this; taking the number, diving it by 2, repeat until the number reaches 0 and the remainders of everything from bottom up is the number in binary (same with diving by 16 for hex).
However, I have absolutely no idea how to implement this into assembly (intel 80186). I've got something in quasi pseudo-code:
1234 / 2 = 617, r 0
617 / 2 = 308, r1
308 / 2 = 154, r0
So much fail that I haven't even thought of how to output this yet. :|
move number into register
divide register (say, AX)'s value by 2
mod ax, 2, and stick that result in DX or something
loop this (cmp ax, 0, jne, dec cx)
The same algorithm works for hex, but then I'd have to worry about weirdly decimal'd remainders and crap. Haven't figured out how to get around that yet.
So I guess what I'm asking is: is there a better way to do this/go about doing this, or can someone grant me a better pseudo code to work with? I'm at a horridly basic level right now with my brain fried...this assignment which you may all find easy is worth a chunk of marks, which really says something about how far along the class is :|
Thanks in advance.
November 16th, 2009, 10:35 PM
So what you're asking for is a better dec -> bin algorithm?
November 17th, 2009, 07:17 AM
Yes and no, I'd be more comfortable using the algorithm up there since I'm familiar with it. I just need a better pseudo code to get things started because I really have no idea which registers I should use, etc.
Originally Posted by Twitch760
If there is a better algorithm though, that would also be nice.
November 17th, 2009, 01:25 PM
I think what you are doing is ok. I'd do something like this.
Originally Posted by idlackage@yahoo
Declare binary as some kind of a string of size 100. Then write something to reverse the elements and then print them out.
while number > 0
mov number, eax
mov ebx, 2
mov binary, edx ;Remainder
mov number, eax ;Quotient
November 17th, 2009, 01:55 PM
Thanks! That really helped me get started. This is what I have now:
I assume I'd just do the same thing with the hex conversion, but then the assignment sheet said that it'd be harder so :| Lost at that part...
mov num, ax ; AX = value of decimal number
mov bx, 2 ; BX = 2
mov cx, 5 ; Counter CX = 5
div bx ; Divide the decimal number by 2
mov result, dx ; Remainder in DX
mov num, ax ; Quotient in AX
loop toBinary ; cmp cx, 0, jne toBinary, dec cx
Before this though I had some code with those registers, using indirect addressing to output the result. So what registers should I use now (or am I overthinking this and it really doesn't matter)?
November 17th, 2009, 05:44 PM
It's similar except you'll be dividing by 16.