Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 28th, 2012, 12:53 PM   #1
vahidjavani
Newbie
 
Join Date: Dec 2012
Posts: 12
Rep Power: 0 vahidjavani is on a distinguished road
Question guide for writing the sum of two hundred digits by Using string.

Hi
I have two numbers (up to hundreds of varieties) to get from the users. And sum it up, I would put in an array.
I had some trouble

First, when two numbers are added together to get an array of cell, and the result is Two digit, I do not know what to do. (because i want to use ADC and I need to single-digit numbers)

Second, the first string put in si, second string put in di. The sum of the string, where I am?
regards

in Persian language:
دو مشکل برای نوشتن جمع دو عدد صد رقمی

سلام
می خوام دو تا عدد (تا صد رقم) از کاربر بگیرم. و *اصل جمع آن ها را، در یک آرایه قرار بدم.
چند تا مشکل داشتم
اول اینکه وقتی دو تا عدد از دو خانه ی آرایه را با هم جمع می کنم و *اصل دو رقمی میشه، نمی دونم باید چیکارش کنم!!


دوم اینکه، رشته اول رو توی si میذارم، رشته دوم رو توی di بعد رشته جواب رو باید چه کار کنم؟؟!!
توی یکی از ثبات ها مثل ax میذارم خطا میده.
vahidjavani is offline   Reply With Quote
Old Dec 29th, 2012, 2:28 AM   #2
The Dark
Programming Guru

 
Join Date: Jun 2005
Posts: 1,631
Rep Power: 14 The Dark will become famous soon enoughThe Dark will become famous soon enough
Re: guide for writing the sum of two hundred digits by Using string.

1. Add from the least significant digit to the most significant digit. When you have a sum of the two digits that is over 9, set a carry flag and subtract 10 from the value. Then, when you add the next 2 digits and there is a carry flag, add 1 to the sum.
e.g.
416
+
739

6+9 = 15. Subtract 10: gives 5 and the carry flag set
1 + 3 = 4. Add one for the carry flag gives 5 and the carry flag clear
4 + 7 = 11. Subtract 10: gives 1 and the carry flag set
As you finished with the carry flag you need to start with a 1.
So the answer would be:
1155

2. You would need to use another register, or a memory location and use a register for an offset. Make sure that your output location is at least one bigger than your inputs.
The Dark is offline   Reply With Quote
Old Jan 2nd, 2013, 9:39 AM   #3
vahidjavani
Newbie
 
Join Date: Dec 2012
Posts: 12
Rep Power: 0 vahidjavani is on a distinguished road
Question debug code

Many thanks for the answer The Dark

Please help me debug this code
http://www.mediafire.com/download.php?o79xx1mkh86e2o7
INCLUDE io.h ; header file for input/output

cr EQU 0dh ; carriage return character
Lf EQU 0ah ; linefeed character

stack SEGMENT STACK
DW 100h DUP(?)
stack ENDS

data SEGMENT
number1 DB 101 DUP(?)
prompt1 DB "Enter first number: ", 0
prompt2 DB "Enter second number: ", 0
number2 DB 101 DUP(?)
answer DB "answer is"
sum DB 102 dup(?)
DB 0
data ENDS
code SEGMENT
ASSUME cs:code, ds:data

start: mov ax, SEG data ;load data segment number
mov ds, ax
mov es, ax

prompt: output prompt1 ;prompt for first number
inputs number1, 100 ;read ASCII characters
dec cx
lea si,number1
add si,cx
mov ax,cx
output prompt2 ;prompt for second number
inputs number2, 100 ;read ASCII characters
lea di,number2
add di,ax
lea bx,sum
add bx,ax
lable: atoi [si]
mov bx,ax
atoi [di]
adc ax,bx
cmp ax,10
jl contin
sub ax,10
stc
contin: itoa [bx],ax
dec di
dec si
dec bx
clc
loop lable
output answer

quit: mov al, 0 ;return code 0
mov ah, 4ch ;DOS function to return
int 21h ;interupt for DOS services

code ENDS
END start
vahidjavani is offline   Reply With Quote
Old Jan 2nd, 2013, 5:26 PM   #4
The Dark
Programming Guru

 
Join Date: Jun 2005
Posts: 1,631
Rep Power: 14 The Dark will become famous soon enoughThe Dark will become famous soon enough
Re: guide for writing the sum of two hundred digits by Using string.

You should say what it is doing wrong to help me debug it.
Here are a few issues:
  1. bx is reused as a temporary in the addition, then it is used as the output location.
  2. You have assumed that both numbers have the same length
  3. You need to leave an extra space in the output for the first '1' if there is an overflow.
The other thing you should do is to step through it in a debugger and see if you are getting the right values in your registers and memory locations.
The Dark is offline   Reply With Quote
Old Jan 2nd, 2013, 10:27 PM   #5
sixstringartist
Programming Guru

 
Join Date: Jun 2005
Posts: 1,333
Rep Power: 13 sixstringartist will become famous soon enough
Re: guide for writing the sum of two hundred digits by Using string.

I don't see where CX is ever initialized. Is the input function doing this? Im used to seeing eax used for return values.
sixstringartist is offline   Reply With Quote
Old Jan 2nd, 2013, 10:37 PM   #6
The Dark
Programming Guru

 
Join Date: Jun 2005
Posts: 1,631
Rep Power: 14 The Dark will become famous soon enoughThe Dark will become famous soon enough
Re: guide for writing the sum of two hundred digits by Using string.

I thought that too, but assumed it was the returned length from the inputs call.
The Dark is offline   Reply With Quote
Old Jan 3rd, 2013, 11:40 AM   #7
sixstringartist
Programming Guru

 
Join Date: Jun 2005
Posts: 1,333
Rep Power: 13 sixstringartist will become famous soon enough
Re: guide for writing the sum of two hundred digits by Using string.

What does your input look like once it gets read in by inputs? are you handling the existence of a newline at the end of your input?
sixstringartist is offline   Reply With Quote
Old Jan 4th, 2013, 6:21 AM   #8
vahidjavani
Newbie
 
Join Date: Dec 2012
Posts: 12
Rep Power: 0 vahidjavani is on a distinguished road
Question Re: guide for writing the sum of two hundred digits by Using string.

Quote:
Originally Posted by sixstringartist View Post
What does your input look like once it gets read in by inputs? are you handling the existence of a newline at the end of your input?
Yes, it was a bug, which I think is in debug code
But still no output!
Please help
INCLUDE io.h ; header file for input/output

cr EQU 0dh ; carriage return character
Lf EQU 0ah ; linefeed character

stack SEGMENT STACK
DW 100h DUP(?)
stack ENDS

data SEGMENT
number1 DB 101 DUP(?)
prompt1 DB "Enter first number: ", 0
prompt2 DB "Enter second number: ", 0
number2 DB 101 DUP(?)
answer DB "answer is"
sum DB 102 dup(?)
DB 0
tlable DB cr,lf,0
temporary DB 10 dup(?)
DB cr,lf,0

data ENDS
code SEGMENT
ASSUME cs:code, ds:data

start: mov ax, SEG data ;load data segment number
mov ds, ax
mov es, ax

prompt: output prompt1 ;prompt for first number
inputs number1, 100 ;read ASCII characters
dec cx
lea si,number1
add si,cx
mov ax,cx
output prompt2 ;prompt for second number
inputs number2, 100 ;read ASCII characters
lea di,number2
add di,ax
lea dx,sum
add dx,ax
lable: atoi [si]
mov bx,ax
atoi [di]
adc ax,bx
cmp ax,10
jl contin
sub ax,10
stc
contin: itoa temporary,ax
mov [dx],temporary
mov [di],0
mov [si],0
dec di
dec si
dec dx
clc
loop lable
output answer

quit: mov al, 0 ;return code 0
mov ah, 4ch ;DOS function to return
int 21h ;interupt for DOS services

code ENDS
END start
vahidjavani is offline   Reply With Quote
Old Jan 23rd, 2013, 7:33 AM   #9
vahidjavani
Newbie
 
Join Date: Dec 2012
Posts: 12
Rep Power: 0 vahidjavani is on a distinguished road
Exclamation Re: guide for writing the sum of two hundred digits by Using string.

Please help me to debug this code
I got an error in line 51.: illegal indexing mode

INCLUDE io.h ; header file for input/output

cr EQU 0dh ; carriage return character
Lf EQU 0ah ; linefeed character

stack SEGMENT STACK
DW 100h DUP(?)
stack ENDS

data SEGMENT
number1 DB 101 DUP(?)
prompt1 DB "Enter first number: ", 0
prompt2 DB "Enter second number: ", 0
number2 DB 101 DUP(?)
answer DB "answer is"
sum DB 102 dup(?)
DB 0
tlable DB cr,lf,0
temporary DB 10 dup(?)
DB cr,lf,0

data ENDS
code SEGMENT
ASSUME cs:code, ds:data

start: mov ax, SEG data ;load data segment number
mov ds, ax
mov es, ax

prompt: output prompt1 ;prompt for first number
inputs number1, 100 ;read ASCII characters
dec cx
lea si,number1
add si,cx
mov ax,cx
output prompt2 ;prompt for second number
inputs number2, 100 ;read ASCII characters
lea di,number2
add di,ax
lea dx,sum
add dx,ax
lable: atoi [si]
mov bx,ax
atoi [di]
adc ax,bx
cmp ax,10
jl contin
sub ax,10
stc
contin: add ax,30h
mov [dx],ax ; //////////////////////////////////////////////////////////////////// Error line 51 illegal indexing mode
mov [di],0
mov [si],0
dec di
dec si
dec dx
clc
loop lable
output answer

quit: mov al, 0 ;return code 0
mov ah, 4ch ;DOS function to return
int 21h ;interupt for DOS services

code ENDS
END start
vahidjavani is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Double data when saving Serigraphie Visual Basic .NET 2 Aug 1st, 2012 4:37 AM
sum of 2 digits mistake in code? 3drenderer Assembly 6 Feb 16th, 2011 6:31 AM
Converting Capitals to lowercase in a string - MIPS Nukkenfutz Assembly 1 Dec 2nd, 2010 8:46 AM
Sum of digits in a float? maligree C 3 Jul 12th, 2008 3:37 PM
An Attempt at a DBMS grimpirate PHP 8 Apr 17th, 2007 1:01 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:55 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC