can't figure out why keep getting an 'illegal operation' message or infinite loop on this ascii to binary conversion program

Posted on 2004-11-06
Last Modified: 2006-11-17
; when i run this code i get either an illegal operation notice or an infinite loop
.model small          

.stack 100h
count = 50          

     maxInput db count
     inputCount db ?          
     buffer db count dup(?)

input KEYBOARD <>    
start db "Enter an integer between -32,768 and 32,767: $"
continue db "Would you like to continue?(y/n) $"    

main PROC          
mov ax, @data          
mov ds, ax          

mov ah, 9          ; write a '$' terminated string to standard output
mov dx, offset start     ; load the string to be output
int 21h              

mov ah, 0Ah          ; read an array of buffered characters frm std input
mov dx, offset input     ; load the address to store the input
int 21h              

mov ah, 6          ; write a character to standard output
mov dl, 0Ah          ; output a newline
int 21h              

mov cx, 0          ; clear the counter
mov cl, input.inputCount     ; number of bytes in input string
mov si, offset input.buffer          ; point to the input string

printNumber:          ; loop used to output the input
     mov dl, byte ptr [si]     ; mov the number to be printed to 'dl'
     int 21h          
     inc si          
     loop printNumber

     mov dl, 09h     ; print a tab
     int 21h    

mov si, offset input.buffer          ; point to the input string
mov ax, 0          ; clear the accumulator
mov bx, 10          ; bx is the multiplier
mov cx, 0          ; clear the counter
mov cl, input.inputCount     ; number of bytes in input string

Convert:          ; loop used to convert ASCII input to binary
     and byte ptr [si], 00001111b     ; actual conversion
     mul bx          
     add al, byte ptr [si]    
     loop Convert    

mov cx, 16          ; count 16 bit output

binloop:          ; loop used to output binary numbers
     .IF cx == 8          ; if 8 bits have been output
          mov ah, 6     ; write a character to standard output
          mov dl, 20h     ; print 'space'
          int 21h          

     shl ax, 1          ; capture bit in carry flag
     jc put1               ; if it's a '1', go to put1
     mov dl, '0'          ; load a '0'
     int 21h              
     jmp proceed          ; go to proceed

     mov dl, '1'          ; load a '1'
     int 21h              
     loop binloop          

main ENDP          
mov ah, 4ch          
int 21h              

END main          
Question by:zeviddalop
    LVL 22

    Accepted Solution

    At the very least, your shl ax is going  to mess up the function code in ah.
    LVL 3

    Assisted Solution

    As grg99 said, you're using the same register as the function code in AH and the accumulator for your value. I notice you're not using SI for anything; just move the value into SI before the loop, and use that instead of AX.

    Hint: Instead of the conditional jump, use ADC - Add with Carry.

    mov dl,0
    shl si,1    ;Will set the carry flag
    adc dl,'0'  ;Adds '0' if no carry, or '1' if carry
    int 21h

    This way, instead of duplicating code and having two jumps (one conditional), you have three statements that get it straight in.
    LVL 61

    Expert Comment

    No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

    I will leave the following recommendation for this question in the Cleanup topic area:
        Accept: grg99 {http:#12519651} & Rosuav {http:#12889515}

    Any objections should be posted here in the next 4 days. After that time, the question will be closed.

    EE Cleanup Volunteer

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Patching a Windows EXE 23 727
    Assemlby_ARM 3 503
    Binary Bomb: Phase 4 func4 3 253
    MIPS Assembly 8 519
    Local Continuous Replication is a cost effective and quick way of backing up Exchange server data. The following article describes the steps required to configure Local Continuous Replication. Also, the article tells you how to restore from a backup…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now