Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: programming
I am working on an assignment, and I`ve been encountering divide overflow after entering my input. Below, I`ve attached what I am supposed to do (on the top of my code), and I`ve attached my code.
; Number Conversion Program
; Requirement #1: Your program should clearly prompt the user with a message to enter an
exactly 4 digit decimal number, which you should store as a nullterminated string (i.e., 5 characters total).
Requirement #2: If the user types fewer than 4 digits before pressing return, then your
program should ask for a 4 digit number again.  Otherwise, your program
should efficiently convert the 4 character numeric string into a 16-bit
numeric value and store this in a word variable.
Requirement #3: Using a loop, convert the 16-bit value into a 16 character string of ‘0’s and
‘1’s.  Store this in another null-terminated string.
 org 100h
section .text
        mov bx, input           ;get address of first char for input
        mov cx, 2
        mov ah, 00h             ;service 00h (get keystroke)
        int 16h                         ;call interrupt 16h (AH=00H) and character read will now be in AL
        cmp al, 0Dh             ;check if user pressed enter
        je convert                      ;if so, convert
        mov [bx], ax            ;if not, store character
        inc bx                          ;point BX to next char of input
        jmp myloop                      ;repeat for next character
        div cx                          ;dividing the number by 2 since binary is base 2
        add dx, 30h
        cmp ax, 0
        je output
        mov [bx], al
        inc bx
        jmp convert
    mov dl, [bx]    ; get char at address in BX
    inc bx          ; point BX to next char in message
    cmp dl, 0       ; Is DL = null (that is, 0)?
    je  quit        ; If yes, then quit
    mov ah, 06      ; If no, then call int 21h service 06h
    int 21h         ;    to print the character
    jmp myloop      ; Repeat for the next character
        int 20h                         ;quit program
section .data
        input db 0, 0, 0, 0, 0
Posted 10-Nov-12 15:20pm
Jason Gleim at 10-Nov-12 22:08pm
Overflows almost always come from loops in the code or adding large numbers and trying to put the sum in a register that is too small. If you run the app with small numbers and it still overflows, it is probably a loop. (hint... hint)
Since this looks like a homework assignment though... don't expect anyone to tell you where the error is. We don't do homework assignments... you don't learn anything that way.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Why the heck are you using DIV to divide by two? Why not shift the number one place to the right instead:[^] - and that's ignoring that you don't need to divide by two in the first place...
A single bit shift right is a divide by 2, and single shift left is a multiply by two.
But the description of your homework and the code you show us do not match in any way that I can see!
If you are converting from a string to a binary number, then the most obvious way is to loop through the ASCII digits from the most significant (left-most) to the least significant (right-most), converting each to a number (by subtracting 30h), multiply the current total by ten, and adding the digit number.
Since this is your homework, I'll give you no code.


  Print Answers RSS
0 Sergey Alexandrovich Kryukov 305
1 OriginalGriff 225
2 DamithSL 130
3 Kornfeld Eliyahu Peter 130
4 Peter Leow 95
0 OriginalGriff 7,355
1 DamithSL 5,199
2 Sergey Alexandrovich Kryukov 4,942
3 Maciej Los 4,906
4 Kornfeld Eliyahu Peter 4,514

Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 11 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid
