flat assembler
Message board for the users of flat assembler.

Index > DOS > Simple Arithmetics

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



Joined: 21 Feb 2010
Posts: 252
adroit 17 Mar 2010, 20:50
I want to use simple expressions such as A = 1 + 3 in my program. How do I implement this into assembly?

_________________
meshnix
Post 17 Mar 2010, 20:50
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4347
Location: Now
edfed 17 Mar 2010, 20:54
Code:
A dd ?
...
mov eax,1
add eax,3
mov [A],eax
    
Post 17 Mar 2010, 20:54
View user's profile Send private message Visit poster's website Reply with quote
adroit



Joined: 21 Feb 2010
Posts: 252
adroit 17 Mar 2010, 23:04
Not like that! I meant using base 10 numbers. Like adding 1 and 3 to give you 4 (1th place).
Post 17 Mar 2010, 23:04
View user's profile Send private message Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 17 Mar 2010, 23:08
Post 17 Mar 2010, 23:08
View user's profile Send private message Reply with quote
adroit



Joined: 21 Feb 2010
Posts: 252
adroit 18 Mar 2010, 00:17
Um, something like that.
Check out this algorithm:

Code:
A = 1
B = 2

Total = A + B
PRINT Total
    


This would work the same in assembly, where decimals are added, subtracted, divided, or multiplied to give a result (in decimal, and only decimal)
Post 18 Mar 2010, 00:17
View user's profile Send private message Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 18 Mar 2010, 01:31
This is exactly what edfed's example does. The result is available in both the eax register and the A memory location.
In your example, the Total result is probably converted to ASCII by the PRINT function.

And there's no such thing as a "decimal and only decimal" notation. 65 (in decimal) = 41 in hexadecimal = 101 in octal = 1000001 in binary = A in ASCII, etc. It's like saying "Hi!" in 10 different languages. It still means the same.

You should check decimal to ASCII conversion code. There are many available on this forum and elsewhere.
Post 18 Mar 2010, 01:31
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1903
DOS386 18 Mar 2010, 06:58
MeshNix wrote:
Not like that! I meant using base 10 numbers. Like adding 1 and 3 to give you 4 (1th place).


See above. The code does exactly that Idea

Maybe you search for decimal input [1] and output [2] converison ?

[1]

Code:
lea ecx, [ecx*5]             ; %
lea ecx, [ecx*2 + eax - 48]  ; % LEANTQ ECX, [ECX*10 + VAL(AL$)]
    


http://forum.nasm.us/index.php?topic=454.0 "Can somebody explain me this code?" 2009-04-11

[2] See some of my 1'000'000'000 examples Smile

_________________
Bug Nr.: 12345

Title: Hello World program compiles to 100 KB !!!

Status: Closed: NOT a Bug
Post 18 Mar 2010, 06:58
View user's profile Send private message Reply with quote
adroit



Joined: 21 Feb 2010
Posts: 252
adroit 19 Mar 2010, 15:38
Thanks!
Post 19 Mar 2010, 15:38
View user's profile Send private message Reply with quote
sandman_1



Joined: 19 Mar 2010
Posts: 16
sandman_1 20 Mar 2010, 16:28
DOS386 wrote:


http://forum.nasm.us/index.php?topic=454.0 "Can somebody explain me this code?" 2009-04-11



int F(int a, int b)
{
return a+b*25;
}

[/code]

Code:
push      ebp
mov       ebp,esp

   mov       eax,dword ptr [ebp+12]
   lea       eax,dword ptr [eax+4*eax]
   lea       eax,dword ptr [eax+4*eax]
   add       eax,dword ptr [ebp+8]

pop       ebp
   ret
    


Per __cdecl calling convention, the parameters are pushed on the stack in reverse order.

Before the function is called, they are pushed on the stack like below.

Code:
push int b
push int a
    


When the call to the function is made, the instruction pointer is saved by pushing it on the stack.
Code:
call F          ;EIP saved and pushed on stack
    


Next the ebp is saved and pushed on the stack. Stack pointer is moved into it for operation.
Code:
push      ebp
mov       ebp,esp
    


The stack is aligned 4 bytes so here is how things are ordered on the stack.
Code:
ebp+12 = int b
ebp+8  = int a
ebp+4  = Saved EIP value
ebp+0  = Saved EBP value
    


Now lets say b = 2 and a = 3. The result should be 2*25+3 = 53.

Code:
 mov       eax,dword ptr [ebp+12]     ;ebp+12 = int b or in this case 2
 lea       eax,dword ptr [eax+4*eax] ;basically 2*4+2=10 eax=10 now
 lea       eax,dword ptr [eax+4*eax] ;basically 10*4+10= 50 eax=50 now
 add       eax,dword ptr [ebp+8]       ;basically 50+3=53 eax=53 now

pop       ebp                                   ;restore ebp to previous value
   ret                                              ;EIP restored and incremented to next instruction following Call to F function


;Stack clean up code. C functions require caller to clean up stack
add esp, 8                   ;2 parameters pushed on stack 4 bytes each
    


Note that C functions use the eax register to return values. Check that for the function return value.
Post 20 Mar 2010, 16:28
View user's profile Send private message Reply with quote
sandman_1



Joined: 19 Mar 2010
Posts: 16
sandman_1 20 Mar 2010, 16:41
Integer to ASCII conversion:

1 .Get an integer
2. Divide by 10
3. Check remainder. This is your right most digit
4. Divide result, not remainder, by 10
5. Check remainder. This is your next digit
6. repeat 4 and 5 until result is less than 10. This is your left most digit.

example:

4569/10 = 456 Remainder 9
456/10 = 45 Remainder 6
45/10 = 4 Remainder 5
4<10 = This is final digit

ASCII to Integer:

1. Get right most ASCII digit
2. Sub from 0x30
3. multiply by 10^0
4. take next digit
5. multiply by next power of 10
6. Add to previous result
7. repeat 4-6 until done
Post 20 Mar 2010, 16:41
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 20 Mar 2010, 17:14
Slightly less work version of ASCII to Integer:

1: result = 0
2: result = result * 10
3: Get left most ASCII digit
4: Sub 0x30
5: Add to previous result
6: repeat 2-5 until done
Post 20 Mar 2010, 17:14
View user's profile Send private message Visit poster's website Reply with quote
adroit



Joined: 21 Feb 2010
Posts: 252
adroit 20 Mar 2010, 17:40
Impressive! Just I was looking for.
This will give me a lead!
Post 20 Mar 2010, 17:40
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1903
DOS386 21 Mar 2010, 00:47
sandman_1 wrote:
http://forum.nasm.us/index.php?topic=454.0 "Can somebody explain me this code?" 2009-04-11
Code:
int F(int a, int b) 
{
 return a+b*25;
}
    

Code:
push      ebp
mov       ebp,esp
   mov       eax,dword ptr [ebp+12]
   lea       eax,dword ptr [eax+4*eax]
   lea       eax,dword ptr [eax+4*eax]
   add       eax,dword ptr [ebp+8]
    



The lower post would be more interesting Wink Also, about the upper post, the ptr's are not needed for FASM nor for NASM Wink

Also, this COOL LEA hack will work with 32-bit code only, not for Real Mode 16-bit code Neutral
Post 21 Mar 2010, 00:47
View user's profile Send private message Reply with quote
sandman_1



Joined: 19 Mar 2010
Posts: 16
sandman_1 21 Mar 2010, 19:35
DOS386 wrote:
sandman_1 wrote:
http://forum.nasm.us/index.php?topic=454.0 "Can somebody explain me this code?" 2009-04-11
Code:
int F(int a, int b) 
{
 return a+b*25;
}
    

Code:
push      ebp
mov       ebp,esp
   mov       eax,dword ptr [ebp+12]
   lea       eax,dword ptr [eax+4*eax]
   lea       eax,dword ptr [eax+4*eax]
   add       eax,dword ptr [ebp+8]
    



The lower post would be more interesting Wink Also, about the upper post, the ptr's are not needed for FASM nor for NASM Wink

Also, this COOL LEA hack will work with 32-bit code only, not for Real Mode 16-bit code Neutral


I was under the impression you wanted the code explained? I am aware the ptrs are not needed in FASM. I posted the code verbatim.
Post 21 Mar 2010, 19:35
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1903
DOS386 22 Mar 2010, 07:23
sandman_1 wrote:
I was under the impression you wanted the code explained?


NO. See original topic in NASM forum.
Post 22 Mar 2010, 07:23
View user's profile Send private message Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 790
Location: Adelaide
sinsi 22 Mar 2010, 07:52
>Also, this COOL LEA hack will work with 32-bit code only, not for Real Mode 16-bit code
Will still work in real-mode 32-bit code though.
Post 22 Mar 2010, 07:52
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 22 Mar 2010, 13:23
I would like to propose a better solution to this:
Code:
   push      ebp
   mov       ebp,esp
   mov       eax,dword ptr [ebp+12] ; clock 1+1 latency
   lea       eax,dword ptr [eax+4*eax] ; clock 3+1 l
   lea       eax,dword ptr [eax+4*eax] ; clock 5+1 l
   add       eax,dword ptr [ebp+8] ; clock 7+1 l
    

Code:
   push      ebp
   mov       ebp,esp
   imul      eax,dword ptr [ebp+12],25 ; clock 1+3 latency
   add       eax,dword ptr [ebp+8] ; clock 5+1 l
    

smaller and faster. IMUL isn't that bad Smile
Post 22 Mar 2010, 13:23
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 22 Mar 2010, 13:26
Madis731 wrote:
smaller and faster. IMUL isn't that bad Smile
It depends upon which processor you are talking about. IMUL can be very bad on some CPUs. IMUL can be really good on other CPUs.
Post 22 Mar 2010, 13:26
View user's profile Send private message Visit poster's website Reply with quote
sandman_1



Joined: 19 Mar 2010
Posts: 16
sandman_1 22 Mar 2010, 14:41
DOS386 wrote:


NO. See original topic in NASM forum.


??? I got the code from the original poster. I am baffled to why you posted a link to it and inserted "please explain this", but don't want it explained. Ok, I guess I wasted my time explaining it.
Post 22 Mar 2010, 14:41
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 22 Mar 2010, 14:49
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.
Post 22 Mar 2010, 14:49
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.