flat assembler
Message board for the users of flat assembler.

 Index > DOS > Simple Arithmetics Goto page Previous  1, 2, 3  Next
Author
sandman_1

Joined: 19 Mar 2010
Posts: 16
sandman_1 22 Mar 2010, 15:48
revolution wrote:
sandman_1: It seems you didn't quite get what was meant. DOS386 posted "Can somebody explain me this code?" because that was the title of the thread that was linked. Somebody in another website posted the topic and gave it that title.

OK, my bad. Didn't realize that.
22 Mar 2010, 15:48
LocoDelAssembly

Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 22 Mar 2010, 17:37
Yet, I think it was good to have the explanation here, DOS386 know, but other people looking for "Simple Arithmetic" will probably learn something new from it.
22 Mar 2010, 17:37

Joined: 21 Feb 2010
Posts: 252
23 Mar 2010, 22:59

Joined: 21 Feb 2010
Posts: 252
W A R N I N G___O L D P O S T !

I'm back! (Sorry)
I still have a few questions.
When I mentioned ASCII conversion, I meant converting 255 (decimal) to "255" (ASCII).

Or
should I just devise an algorithm code to perform basic arithmetics on a string.
Example:
Code:
```"1234" + "5678" = "6912"
```

Where strings will be added together, to give result (using a coded algorithm).
09 May 2010, 22:11
Tyler

Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 09 May 2010, 22:34
Do arithmetic on numbers(not ASCII) and convert to ASCII. To do the problem above, convert to numbers, then do the arithmetic, then convert back to ASCII.
Code:
```; si = string number
; returns number in eax
str_to_int:
push edx
xor  edx,edx
.loop:
xor  eax,eax
lodsb
cmp  al,0
je   .done
sub  al,'0'
imul edx, 10 ; substitute 10 for any base you want, but it wont handle letters correctly
jmp  .loop
.done:
mov  eax,edx
pop  edx
ret
```

That should work, I think.
09 May 2010, 22:34
DOS386

Joined: 08 Dec 2006
Posts: 1900
DOS386 10 May 2010, 07:45
MeshNix wrote:
W A R N I N G___O L D P O S T !

Quote:
When I mentioned ASCII conversion, I meant converting 255 (decimal) to "255" (ASCII).

You are still missing the point. "255" (decimal) and "255" (ASCII) is the very same thing.

Quote:
should I just devise an algorithm code to perform basic arithmetics on a string.

This is possible, but possibly not the best idea.

10 May 2010, 07:45

Joined: 21 Feb 2010
Posts: 252
Thanks, DOS86.

Tyler wrote:
Do arithmetic on numbers(not ASCII) and convert to ASCII.

I get it, but after arithmetics are done, I still have to convert it back to ASCII. When I divide by ten, how do I retrieve the remainder? (See, a post by revolution on the 1st page)

DOS86 wrote:

You are still missing the point. "255" (decimal) and "255" (ASCII) is the very same thing.

That wasn't what I meant. 255 decimal is different that (literal) "255" ASCII characters, by:
Code:
```decimal db 255    ; this is a numerical value.
ascii   db "255"  ;this is a sequence of characters (or a string)    ```

_________________
meshnix
22 May 2010, 05:07
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20132
revolution 22 May 2010, 05:27
MeshNix wrote:
When I divide by ten, how do I retrieve the remainder?
See the Intel, or AMD, manual for the DIV instruction. You won't get very far in assembly without knowing what the actual instructions do, so downloading and reading the manuals is a must.

If you just want to be lazy and not read the manuals and simply rely upon, possibly incorrect, information from random forums posts (like this one) then the quotient is in (R/E)ax and the remainder is in (R/E)dx. But don't rely on just what I say, it could be wrong (and is wrong if you use "DIV r8". So probably best if you read the manual to get the full information)
22 May 2010, 05:27

Joined: 21 Feb 2010
Posts: 252
Okay. I have a few datasheets on IA-32.
22 May 2010, 17:54
mindcooler

Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 22 May 2010, 19:43
Can't the BCD instructions help here?
22 May 2010, 19:43
DOS386

Joined: 08 Dec 2006
Posts: 1900
DOS386 23 May 2010, 00:44
mindcooler wrote:
Can't the BCD instructions help here?

Don't know, for me LEA+MUL and DIV instructions are sufficient.

Quote:

decimal db 255 ; this is a numerical value.
ascii db "255" ;this is a sequence of characters (or a string)

"decimal db 255" is "valid" but bad: it is NOT decimal, it's a numerical value for internal CPU work.

Quote:
Okay. I have a few datasheets on IA-32.

Get something on 8086 or 80386, the later stuff is just confusing bloat for the purpose of trivial decimal conversions
23 May 2010, 00:44

Joined: 21 Feb 2010
Posts: 252
I don't know if BCD is appropriate here.

If db 255 isn't a decimal, then what is?
26 May 2010, 16:19
edfed

Joined: 20 Feb 2006
Posts: 4325
Location: Now
edfed 26 May 2010, 17:23
If db 255 isn't a decimal, then what is?

..
it is binary, represented in decimal for the compiler.

in bcd, 255 is invalid.
allowed values are those who, in hexadecimal, don't use alphabtic chars.
then, 10h in bcd = 10 decimal
99h in bcd = 99 decimal
0FFh in bcd = invalid.

if you want a way to display the decimal chars in fixed point or integer:

code below displays a fixed point decimal number in the form 123456.789
it ignores the invisible zeros. 123.456 instead of 000123.456.

it stores the string at value.
zero is the label to the zero derminator for value string.

Code:
```number dd 123456
value db '123456.789' ; tel us to let 3 chars after the dot
zero  db 0               ; zero! the zero string terminator!
fixeddecimal:
mov eax,[number]
mov ecx,10
mov ebx,zero
.loop:
dec ebx
cmp byte[ebx],'.'
jne @f
dec ebx
@@:
cmp ebx,value
jl .end
xor edx,edx
div ecx
or edx,edx ; this is because div and jne @f don't work for me..
jne @f     ;
or eax,eax  ;
jne @f     ;
mov dl,' '-'0'
@@:
mov [ebx],dl
jmp .loop
.end:
ret
```

Last edited by edfed on 26 May 2010, 18:09; edited 1 time in total
26 May 2010, 17:23
windwakr

Joined: 30 Jun 2004
Posts: 827
windwakr 26 May 2010, 18:01
Edfed, have you even tested that?

Floating point numbers can't be worked with like that. I think you mean to do this:

Code:
```number dd 123456
```
26 May 2010, 18:01
edfed

Joined: 20 Feb 2006
Posts: 4325
Location: Now
edfed 26 May 2010, 18:06
yes, sorry, i commented the source on the board, that's why i forgot that .

it works, as is.
26 May 2010, 18:06

Joined: 21 Feb 2010
Posts: 252
I cannot fully understand the code above.
28 May 2010, 13:19
baldr

Joined: 19 Mar 2008
Posts: 1651
baldr 28 May 2010, 18:48
MeshNix,

The code is quite straightforward: value of number is converted to decimal representation using value as pattern (and result buffer too). Effectively that value is interpreted as fixed-point decimal with three fraction digits (i.e. converted value represents number/1000).

Special handling of decimal point inside pattern and leading zeros suppression ain't big challenge too.
28 May 2010, 18:48

Joined: 21 Feb 2010
Posts: 252
Oh. So what happens here is, value is a representation of what actually happened to number. Nice!

baldr wrote:
Special handling of decimal point inside pattern and leading zeros suppression ain't big challenge too.
It isn't so easy for a beginner like me.

Last edited by adroit on 30 May 2010, 18:13; edited 1 time in total
30 May 2010, 17:26
baldr

Joined: 19 Mar 2008
Posts: 1651
baldr 30 May 2010, 17:52
MeshNix,

ebx simply steps over '.'; leading zeroes are handled by this:
Code:
```        or edx,edx ; test for zero digit
jne @f     ; not zero, put it
or eax,eax ; check that zero is leading (i.e. the rest of digits are zero too)
jne @f     ; not leading, put it
mov dl,' '-'0'; adjust dl: ' '-'0'+'0' == ' '; +'0' is from the following instruction
@@:
30 May 2010, 17:52

Joined: 21 Feb 2010
Posts: 252
After a character digit steps over the '.', then a zero is place at the front of value, as it shifts right by this fragment code:

Code:
```...
mov [ebx],dl
...    ```

mov [ebx],dl. ebx point to the start of value, right?
Since, ' '-'0'+'0' = ' ', then ' ' is attached to the front of value, right?

Last edited by adroit on 30 May 2010, 19:20; edited 1 time in total
30 May 2010, 18:48
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area
Goto page Previous  1, 2, 3  Next

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum