[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

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

; when i run this code i get either an illegal operation notice or an infinite loop
;-------------------------------------------------------------------------------------
.model small          

.stack 100h
count = 50          

KEYBOARD STRUCT          
     maxInput db count
     inputCount db ?          
     buffer db count dup(?)
KEYBOARD ENDS          
;(21)-------------------------------------------------------------------------
.data              

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

;(29)-------------------------------------------------------------------------
.code              
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
;(50)-------------------------------------------------------------------------

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
;(66)-------------------------------------------------------------------------

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

     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

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

main ENDP          
;-----------------------------------------------------------------------------
mov ah, 4ch          
int 21h              

END main          
0
zeviddalop
Asked:
zeviddalop
2 Solutions
 
grg99Commented:
At the very least, your shl ax is going  to mess up the function code in ah.
0
 
RosuavCommented:
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.
0
 
mbizupCommented:
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.

mbizup
EE Cleanup Volunteer
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now