#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    34
    Rep Power
    10

    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).

    Code:
    1234 / 2 = 617, r 0
    617 / 2 = 308, r1
    308 / 2 = 154, r0
    ...etc.
    However, I have absolutely no idea how to implement this into assembly (intel 80186). I've got something in quasi pseudo-code:

    Code:
    counter cx
    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)
    So much fail that I haven't even thought of how to output this yet. :|

    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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2007
    Posts
    47
    Rep Power
    0
    So what you're asking for is a better dec -> bin algorithm?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    34
    Rep Power
    10
    Originally Posted by Twitch760
    So what you're asking for is a better dec -> bin algorithm?
    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.

    If there is a better algorithm though, that would also be nice.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2007
    Posts
    47
    Rep Power
    0
    Originally Posted by idlackage@yahoo
    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.

    If there is a better algorithm though, that would also be nice.
    I think what you are doing is ok. I'd do something like this.

    Code:
    while number > 0
    
    mov number, eax
    mov ebx, 2
    divide ebx
    mov binary, edx ;Remainder
    mov number, eax ;Quotient
    Declare binary as some kind of a string of size 100. Then write something to reverse the elements and then print them out.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    34
    Rep Power
    10
    Thanks! That really helped me get started. This is what I have now:

    Code:
    bin_start:
    
    	mov	num, ax		; AX = value of decimal number
    	mov	bx, 2		; BX = 2
    	mov	cx, 5		; Counter CX = 5
    
    toBinary:
    	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
    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...

    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)?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2007
    Posts
    47
    Rep Power
    0
    It's similar except you'll be dividing by 16.

    http://www.permadi.com/tutorial/numDecToHex/

IMN logo majestic logo threadwatch logo seochat tools logo