Hey guys,

The below code asks user for a decimal number (0-65535). It then converts to a 16bit binary number. What I want to do is place a hyphen, every 2 numbers.


Input: 2
Output: 00-00-00-00-00-00-00-10

Here's the code I have so far...It doesn't display the hyphens. That's what I need help on

TITLE MASM Template                        (main.asm)

; Description:
; Revision date:

INCLUDE Irvine32.inc
prompt BYTE "Please Enter Number(0-65535): ",0dh,0ah,0
bin BYTE "Binary (16bit) = ",0

main PROC
    call Clrscr

call readdec

mov edx,OFFSET bin
call WriteString

mov ebx,eax
mov dx, 8000h

mov ecx, 16

    mov ax, dx
    and ax, bx                    ;equivalent of dl = dl & bl
    cmp ax, 0
    jne p_one
    mov eax, 0
    jmp pr_lab
    mov eax, 1
call WriteDec

    shr dx, 1
loop tbin
call crlf

main ENDP

END main

My thought is to use div and compare ah(the remainder) to 0 and then je (jump if equal) to a place where it write's the hyphen.

Thanks in advance!!!

Posted on 2011-09-28 12:37:54 by msmallard
After the call to WriteDec add the following:
test ecx, 1
jz next
cmp ecx, 1
jz next
; Put something that writes a hyphen here

Posted on 2011-09-28 14:49:11 by LocoDelAssembly
You could probably get "div" to work. Are you familiar with the "test" instruction? Since you're using ecx as a loop counter...

test ecx, 1
jz skip_hyphen ; or jnz skip_hyphen

... would jump (or not) every second time through the loop.

Think about how and where you want to print the hyphen. I take it  WriteString wants the offset in edx... and you're already using (e)dx as the "mask". So you might want to preserve edx (if you propose to use WriteString). You could write the hyphen (every second time) before or after WriteDec... If Kip Irvine's library has a "WriteChar" that takes the character in al, that might work out better (since you set eax each time through the loop anyway). You'll probably find that you wind up with too many hyphens (before the number or after the number, depending on how you do it), so you'll probably need to compare ecx with 16 or 1 (and skip the hyphen) either way.

Try "test" - I think it might work out better than "div" for you. ("div" is very slow! Not that it will make much difference.)

test dx, 6
jz skip_hyphen

Might be another approach...

P.S. I see LocoDelAssembly has posted a similar idea... I'll post anyway...

Posted on 2011-09-28 15:06:54 by fbkotler
Thanks for both posts! I'm just diving into the assembly and haven't learned whole lot. That was the reason I was resorting to div.


Once again, thanks for the help!
Posted on 2011-09-28 15:45:14 by msmallard