program should convert BCD->binary, convert, output WHERE is the syntax error?

Started by fdig, February 19, 2006, 06:02:54 PM

Previous topic - Next topic

fdig

Hi,

program logic should be ok, but program doesn't work as assumed.
Perhaps anyone has an idea where mistake may be, programm is well documented.

Programm reads BCD-encoded ASCII number, converts to binary, converts back to - ready to output - BCD-encoded ASCII number. It's a little exercise to learn and play. ;)

To convert BCD-encoded ASCII to binary we need to do following steps:

2345 = ((2 * 10 + 3) * 10 + 4) * 10 + 5

To convert binary number to BCD-encoded ASCII we need to do these steps:

2345 : 10 = 234 remain 5
234 : 10 = 23 remain 4
   23 : 10 = 2 remain 3
     2 : 10 = 0 remain 2



Here it is:


.MODEL small

.STACK 128

.CODE

START:


         ; PART 1
         ; first part stores input (BCD-formatted ASCII number) in binary form
         ;


        mov bx, 10 ; factor to multipy
        mov cx, 0

M1:
        mov ah, 01H ; read digit
        int 21H

        cmp al, 0DH ; is entered digit it Return and we have finished reading in?
        je NEWLINE ; then jump to converting binary -> ASCII BCD-Form

        push ax         ; store lastly read digit to stack, MUL needs ax
        mov ax, cx     ; store whole number temporary in ax
        mul bx          ; multiply number with 10

        pop cx            ; get our lastly read digit to add it to number
        add cx, ax
        jmp M1       ; go on

     






      ; PART 2
      ; converting binary stored number (cx) to BCD-encoded ASCII number to output it
      ;



NEWLINE:
        mov ax, cx
        mov cx, 0    ; used to count stack operations

M3:  mov dx, 0
        mov bx, 10      ; divide with 10 (ax / 10)
        div bx

        push dx        ; store remain (inverted order, thus storing to stack, then output)
        inc cx           ; count remains

        cmp ax, 0       ; do we have result = 0 remain n ?
        jnz M3           

        ; let's output what we have

M4:  pop dx
        add dl, 30H      ; add 30H to get BCD-encoded ASCII
        mov ah, 02H
        int 21H
        loop M4           ; count stored in cx
               
        mov ah, 4CH
        int 21H

        END START



(input = 1)
c:\>test
305


Who did find the mistake?

.asm file is attached, but comments are german only.

Many thanks!

[attachment deleted by admin]

raymond

You can expect the CX register to be trashed when you use INT 21 (or any other INT for that matter, which also applies for AX and DX). Try the following modification to your code.

M4:  pop dx
        add dl, 30H      ; add 30H to get BCD-encoded ASCII
        mov ah, 02H
        push cx        ;save your count
        int 21H
        pop  cx        ;retrieve it
        loop M4           ; count stored in cx


Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com