flat assembler
Message board for the users of flat assembler.
Index
> DOS > Simple Arithmetics Goto page Previous 1, 2, 3 Next |
Author |
|
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 |
|
adroit 23 Mar 2010, 22:59
LocoDelAssembly, I got it
http://forum.nasm.us/index.php?topic=454.0 |
|||
23 Mar 2010, 22:59 |
|
adroit 09 May 2010, 22:11
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 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 add edx,eax jmp .loop .done: mov eax,edx pop edx ret That should work, I think. |
|||
09 May 2010, 22:34 |
|
DOS386 10 May 2010, 07:45
MeshNix wrote: W A R N I N G___O L D P O S T ! Good habit 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. |
|||
10 May 2010, 07:45 |
|
adroit 22 May 2010, 05:07
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:
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 22 May 2010, 05:27
MeshNix wrote: When I divide by ten, how do I retrieve the remainder? 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 |
|
adroit 22 May 2010, 17:54
Okay. I have a few datasheets on IA-32.
|
|||
22 May 2010, 17:54 |
|
mindcooler 22 May 2010, 19:43
Can't the BCD instructions help here?
|
|||
22 May 2010, 19:43 |
|
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" 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 |
|
adroit 26 May 2010, 16:19
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 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' @@: 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 |
|||
26 May 2010, 17:23 |
|
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 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 |
|
adroit 28 May 2010, 13:19
I cannot fully understand the code above.
|
|||
28 May 2010, 13:19 |
|
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 |
|
adroit 30 May 2010, 17:26
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. Last edited by adroit on 30 May 2010, 18:13; edited 1 time in total |
|||
30 May 2010, 17:26 |
|
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 @@: add dl,'0' |
|||
30 May 2010, 17:52 |
|
adroit 30 May 2010, 18:48
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 |
|||
30 May 2010, 18:48 |
|
Goto page Previous 1, 2, 3 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.