flat assembler
Message board for the users of flat assembler.

 Index > DOS > Simple Arithmetics Goto page 1, 2, 3  Next
Author
 Thread
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
17 Mar 2010, 20:50
edfed

Joined: 20 Feb 2006
Posts: 4325
Location: Now
edfed 17 Mar 2010, 20:54
Code:
```A dd ?
...
mov eax,1
add eax,3
mov [A],eax
```
17 Mar 2010, 20:54
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).
17 Mar 2010, 23:04
windwakr

Joined: 30 Jun 2004
Posts: 827
windwakr 17 Mar 2010, 23:08
17 Mar 2010, 23:08
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)
18 Mar 2010, 00:17
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.
18 Mar 2010, 01:31
DOS386

Joined: 08 Dec 2006
Posts: 1900
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

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

_________________
Bug Nr.: 12345

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

Status: Closed: NOT a Bug
18 Mar 2010, 06:58
adroit

Joined: 21 Feb 2010
Posts: 252
adroit 19 Mar 2010, 15:38
Thanks!
19 Mar 2010, 15:38
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.
20 Mar 2010, 16:28
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
20 Mar 2010, 16:41
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20132
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
20 Mar 2010, 17:14
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!
20 Mar 2010, 17:40
DOS386

Joined: 08 Dec 2006
Posts: 1900
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 Also, about the upper post, the ptr's are not needed for FASM nor for NASM

Also, this COOL LEA hack will work with 32-bit code only, not for Real Mode 16-bit code
21 Mar 2010, 00:47
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 Also, about the upper post, the ptr's are not needed for FASM nor for NASM

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

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.
21 Mar 2010, 19:35
DOS386

Joined: 08 Dec 2006
Posts: 1900
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.
22 Mar 2010, 07:23
sinsi

Joined: 10 Aug 2007
Posts: 789
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.
22 Mar 2010, 07:52
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
22 Mar 2010, 13:23
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20132
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
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.
22 Mar 2010, 13:26
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.
22 Mar 2010, 14:41
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20132
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.
22 Mar 2010, 14:49
 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 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

Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.