Sign Up to vote
See more: programming
Hello

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

start:
mov bx, input           ;get address of first char for input
mov cx, 2

myloop:
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

convert:
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

output:
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

quit:
int 20h                         ;quit program

section .data
input db 0, 0, 0, 0, 0```
Posted 10-Nov-12 14:20pm

Comments
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.
Jason Gleim - 10-Nov-12 22:08pm

Sign Up 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: http://en.wikibooks.org/wiki/X86_Assembly/Shift_and_Rotate[^] - 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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Your Filters
 Interested Ignored

 0 OriginalGriff 635 1 Peter Leow 158 2 karthik mahalingam01 147 3 Abhinav S 85 4 joginder-banger 75
 0 Sergey Alexandrovich Kryukov 15,648 1 OriginalGriff 10,351 2 Maciej Los 6,254 3 CPallini 4,530 4 thatraja 4,375

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