flat assembler
Message board for the users of flat assembler.

Index > DOS > Simple Arithmetics

Goto page Previous  1, 2, 3  Next
Author
Thread Post new topic Reply to topic
sandman_1



Joined: 19 Mar 2010
Posts: 16
sandman_1
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.
Post 22 Mar 2010, 15:48
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
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.
Post 22 Mar 2010, 17:37
View user's profile Send private message Reply with quote
adroit



Joined: 21 Feb 2010
Posts: 252
adroit
Post 23 Mar 2010, 22:59
View user's profile Send private message Reply with quote
adroit



Joined: 21 Feb 2010
Posts: 252
adroit
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).
Post 09 May 2010, 22:11
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler
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
   add  edx,eax
   jmp  .loop
   .done:
   mov  eax,edx
   pop  edx
   ret
    

That should work, I think.
Post 09 May 2010, 22:34
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
MeshNix wrote:
W A R N I N G___O L D P O S T !


Good habit Smile Everybody please learn.

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.

Please recheck previous page about decimal OUT and decimal IN conversion.
Post 10 May 2010, 07:45
View user's profile Send private message Reply with quote
adroit



Joined: 21 Feb 2010
Posts: 252
adroit
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
Post 22 May 2010, 05:07
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17278
Location: In your JS exploiting you and your system
revolution
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)
Post 22 May 2010, 05:27
View user's profile Send private message Visit poster's website Reply with quote
adroit



Joined: 21 Feb 2010
Posts: 252
adroit
Okay. I have a few datasheets on IA-32.
Post 22 May 2010, 17:54
View user's profile Send private message Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
Can't the BCD instructions help here?
Post 22 May 2010, 19:43
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
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 Wink
Post 23 May 2010, 00:44
View user's profile Send private message Reply with quote
adroit



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

If db 255 isn't a decimal, then what is?
Post 26 May 2010, 16:19
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4237
Location: 2018
edfed
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.. Sad
        jne @f     ;
        or eax,eax  ;
        jne @f     ;
        mov dl,' '-'0'
@@:
        add dl,'0'
        mov [ebx],dl
        jmp .loop
.end:
        ret
    


Last edited by edfed on 26 May 2010, 18:09; edited 1 time in total
Post 26 May 2010, 17:23
View user's profile Send private message Visit poster's website Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
Location: Michigan, USA
windwakr
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
    
Post 26 May 2010, 18:01
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4237
Location: 2018
edfed
yes, sorry, i commented the source on the board, that's why i forgot that .

it works, as is. Very Happy
Post 26 May 2010, 18:06
View user's profile Send private message Visit poster's website Reply with quote
adroit



Joined: 21 Feb 2010
Posts: 252
adroit
I cannot fully understand the code above.
Post 28 May 2010, 13:19
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
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.
Post 28 May 2010, 18:48
View user's profile Send private message Reply with quote
adroit



Joined: 21 Feb 2010
Posts: 252
adroit
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
Post 30 May 2010, 17:26
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
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
@@:
        add dl,'0'    
Post 30 May 2010, 17:52
View user's profile Send private message Reply with quote
adroit



Joined: 21 Feb 2010
Posts: 252
adroit
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:
...
add dl,'0' 
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
Post 30 May 2010, 18:48
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2, 3  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.