Author Topic: to split a 2-digit number i m getting errors  (Read 8595 times)

Offline kruthika

  • Jr. Member
  • *
  • Posts: 3
to split a 2-digit number i m getting errors
« on: January 06, 2013, 11:02:53 AM »
i tried to split a 2-digit number into individual digits for printing...here is the code
Code: [Select]
mov edx, 0
mov ah, 0
mov al, byte[sum]
mov bl, 10
div bl
mov byte[digit2], al
mov byte[digit1], ah

result is coming as '06=4' when it should be only '06'

then i made some changes to the code and i tried it this way
Code: [Select]
mov al, byte[sum]
mov bl, 10
div bl
mov byte[digit2], al
mov byte[digit1], ah

result is shown as '06Floating point error(core dumped)'

please help me to sort out this error

Offline Mathi

  • Jr. Member
  • *
  • Posts: 82
  • Country: in
    • Win32NASM
Re: to split a 2-digit number i m getting errors
« Reply #1 on: January 06, 2013, 11:21:28 AM »
Looks like you are writing a program for DOS.

Can you post the full program.
The error messages you have pasted may not be due to the code blocks you have pasted.

Regards,
Mathi.

Offline kruthika

  • Jr. Member
  • *
  • Posts: 3
Re: to split a 2-digit number i m getting errors
« Reply #2 on: January 06, 2013, 11:36:59 AM »
i m doing it with terminal(ubuntu)
my full program is
Code: [Select]
;Initialized variables

section .data
message: db "Enter any number from 0 to 9: "
message_length: equ $-message


;Un-initialized variables

section .bss
digit1: resb 1
digit2: resb 1
num: resb 1
sum: resb 1

section .text
global _start
_start:

;Printing prompt message
 mov eax, 4
 mov ebx, 1
 mov ecx, message
 mov edx, message_length
 int 80h

;Reading the digit
 mov eax, 3
 mov ebx, 0
 mov ecx, digit1
 mov edx, 2
 int 80h
;Read and ignore an extra character as the system will read enter
;press as well
 int 80h

;Calculating the number from digits
 sub byte[digit1], 30h
 

 mov byte[sum], 0
 mov cl, byte[digit1]

;Initializing sum to zero
;Initializing loop variable(ecx) to number

;Loop for adding cx to num
adding:
 add byte[sum], cl
Loop adding

;The result could be maximum of 2 digits......
;In the remaining section of the code, we will break the
;number so as to print each digit one by one
 
mov al, byte[sum]
mov bl, 10
div bl
mov byte[digit2], al
mov byte[digit1], ah


 
;Converting the digit to its ASCII by adding 30h
 add byte[digit1], 30h
 add byte[digit2], 30h
 
;Printing each digits.............
 
 mov eax, 4
 mov ebx, 1
 mov ecx, digit2
 mov edx, 1
 int 80h

 mov eax, 4
 mov ebx, 1
 mov ecx, digit1
 mov edx, 1
 int 80h
 
 mov eax, 4
 mov ebx, 1
 mov ecx, 0Ah
 mov edx, 1
 int 80h

;Exit code
 mov eax, 1
 mov ebx, 0
 int 80h

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: to split a 2-digit number i m getting errors
« Reply #3 on: January 06, 2013, 12:55:25 PM »
The "floating point error" ("floating point"? I didn't use no floating point! Dos says "divide by zero error". I didn't do that either!) is from the "div". In the first example you show, you zero ah before the "div". Do that.

You've got another error in the last sys_write before you exit...
Code: [Select]
mov ecx, 0Ah
It would be nice if it were that simple, but ecx needs to point to a byte in memory... not "be" that byte. You can put a linefeed in section .data (probably easiest) or do something like...
Code: [Select]
push 0Ah ; LF
mov ecx, esp ; buffer is the stack
mov edx, 1 ; one byte
mov ebx, 1 ; stdout
mov eax, 4
int 80h
add esp, 4 ; clear our "buffer" off the stack
;...

That "div" error is very common. "div" by a byte uses ax, not just al. "div" by a word uses dx:ax. "div" by a dword uses edx:eax. If the result won't fit in 8/16/32 bits, it causes this exception. Beginners never realize this. You'd think they'd fix it! :)

It's almost as easy to handle a full 32-bit number (10 possible digits) as just two digits (your prompt still calls for one!). Good to work up to it a step at a time... Holler if you need more help!

Best,
Frank




Offline TightCoderEx

  • Full Member
  • **
  • Posts: 103
Re: to split a 2-digit number i m getting errors
« Reply #4 on: January 06, 2013, 03:13:35 PM »
I think what you missed is the value of ECX @

Code: [Select]
adding  add byte [sum], cl
           loop adding

Logically, looks good.  If I enter 5 the result should be 5 + 4 + 3 + 2 + 1 = 15 and then you should get 31H and 35H as a result.

Problem is ECX = about 6,291,450 and because adding an 8 bit number will wrap after 255 times, you don't really know what the
result is going to be by the time you want to divide and in some case it is zero

Solution:
Code: [Select]
        mov    byte [sum], 0
        xor      ecx, ecx
        mov     cl, [digit]

This strips the upper 24 bits from ECX and now the counter is the value you expect.

If you were running this program on an 8086, it would probably run just great, but your probably using a 64 bit Linux system.  This means that even though your coding 16 bit, your program sees 64 bits, but because of zero extension, or at least that is what it is on my system 32 bits are significant.  Your still going to get an error when you go to divide because of the contents of EDX and EAX is what you expect either.
« Last Edit: January 06, 2013, 03:27:44 PM by TightCoderEx »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: to split a 2-digit number i m getting errors
« Reply #5 on: January 06, 2013, 08:08:39 PM »
Good catch! I wasn't paying too much attention to what was in "sum". That is, indeed, a problem!

"With enough eyes, all bugs are shallow."

Best,
Frank