flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Windows > Display the result of a registry addition

Author
Thread Post new topic Reply to topic
Mino



Joined: 14 Jan 2018
Posts: 6

Display the result of a registry addition

Hello,
I'm a new in this forum, in FASM and in another assembly languages Wink.

I would like to know how we do a calculation in fasm, and that we display the result. I try something like that:

Code:

mov eax7
add eax3
; Print what's in eax




But is it a good way? And how do you print the result?
I imagine that it's very simple, even that I'm completely stuck ... Please excuse me if it's the case

Thank you Embarassed !

PS: Forgive me also for my English, it's a little rusty xD

_________________
The best way to predict the future is to invent it.
Post 14 Jan 2018, 21:04
View user's profile Send private message Reply with quote
yeohhs



Joined: 19 Jan 2004
Posts: 100
Location: N 5.43564° E 100.3091°

Hi Mino,

Welcome. Very Happy
There are several ways. Here is one way. See attached .asm file.


Description: edited
Download
Filename: integeradd.asm
Filesize: 1.87 KB
Downloaded: 16 Time(s)


_________________
I'm an INTJ aspie.
Post 14 Jan 2018, 21:58
View user's profile Send private message Visit poster's website Reply with quote
Mino



Joined: 14 Jan 2018
Posts: 6

Thank you very much yeohhs Very Happy
The code works perfectly, it remains for me to "study" and understand it to reproduce it without having to copy / paste.
If I still have questions about this code, I will post them on this thread, since they will be linked.
Good evening
Post 15 Jan 2018, 17:09
View user's profile Send private message Reply with quote
yeohhs



Joined: 19 Jan 2004
Posts: 100
Location: N 5.43564° E 100.3091°

You're welcome, Mino. Smile Sure, do post if you more questions. Good evening to you too.

By the way, you can also see the contents of all registers and more details in a debugger.

_________________
I'm an INTJ aspie.
Post 16 Jan 2018, 03:11
View user's profile Send private message Visit poster's website Reply with quote
Mino



Joined: 14 Jan 2018
Posts: 6

Hello again,
I just want to ask you a little question, I use this code to generate a loop (normally infinite) that displays "hello":

Code:

format PE Console 
include 'stdlib.inc' ; A file contain all I need (with the idata section + libs)
entry main
section '.rdata' data readable
   val db "hello"100
section '.code' code executable
   main:
      @@:
         push ecx
         push val
         call [printf]
         pop ecx
         sub ecx1
         jnz @B
call [ExitProcess]





(Tell me if there are bad things to fix)

The program, once compiled, works and displays so loop "hello". However, after x iterations, the program stops and it crashs.
Why Sad ?
Post 17 Jan 2018, 20:26
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 959

printf is a "vararg" function (takes a variable number of arguments), which doesn't pop the args by itself. You'll have to do that yourself. So you will run out of stack space (each loop iteration pushes 'val' and never pops it).

Also, use dec ecx instead of sub ecx, 1 Wink


Code:
format PE Console 
include 'stdlib.inc' ; A file contain all I need (with the idata section + libs)
entry main
section '.rdata' data readable
   val db "hello"100
section '.code' code executable
   main:
      @@:
         push ecx
         push val
         call [printf]
         pop ecx  ; just a dummy pop, smaller than add esp, 4 in encoding
         pop ecx  ; restore ecx
         dec ecx
         jnz @B
call [ExitProcess]

Note that "cdecl" functions also have the same calling convention as printf (i.e. you have to clean the stack). However, most Windows API functions clean it themselves, but always keep in mind if needed. Smile

BTW it's not an infinite loop at all, as you don't know the value of 'ecx' and never set it, or am I missing something? It will have whatever the Windows PE loader places in ecx.
Post 17 Jan 2018, 21:36
View user's profile Send private message Reply with quote
yeohhs



Joined: 19 Jan 2004
Posts: 100
Location: N 5.43564° E 100.3091°


Furs wrote:
printf is a "vararg" function (takes a variable number of arguments), which doesn't pop the args by itself. You'll have to do that yourself. So you will run out of stack space (each loop iteration pushes 'val' and never pops it).



Yes. Smile

To make the loop infinite, I suppose you had to set ECX to a non-zero value somewhere. And if you preserve that value, never change it or never allow it to be changed, jnz @B will always be true.

About loops, Kip Irvine wrote:

Quote:

A common programming error is to inadvertently initialize ECX to zero before beginning a loop. If this happens, the LOOP instruction decrements ECX to FFFFFFFFh, and the loop repeats 4,294,967,296 times! If CX is the loop counter (in real-address mode), it repeats 65,536 times.


_________________
I'm an INTJ aspie.
Post 17 Jan 2018, 23:28
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15383
Location: Monstropolis


Furs wrote:
Also, use dec ecx instead of sub ecx, 1 Wink

Just use loop.
Post 18 Jan 2018, 01:28
View user's profile Send private message Visit poster's website Reply with quote
Mino



Joined: 14 Jan 2018
Posts: 6

Good evening, and thank you for all your answers!
I applied your advice, however I still have 2 questions:
1) What is loop ?
2) If the assembler, here FASM, is supposed to be very fast, why a simple program coded in VB.NET gets to go faster than the code you post?
Here it is :

Code:

Console.WriteLine("hello")



The difference is not huge, but we know how to distinguish it even with the naked eye

Thank you in advance Very Happy
Post 18 Jan 2018, 19:11
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 959

Because you use printf? Just because you use asm doesn't mean that the functions you call are optimized, it's the exact same printf that you use in C or C++ because you link to the same library (msvcrt.dll or whatever).

And of course, asm needs some effort to be fast, as a beginner you should just focus on learning it. There's no point optimizing for micro-architectural stuff when you're a beginner, you'll just confuse yourself, just do some simple guidelines.

Also, loop is an instruction.


I mean, asm is what the CPU executes sort of. So only instructions (and some macro stuff depending on assembler) are part of the "language". printf is just a function in a library (DLL), not part of the "language" unlike in HLLs (but they ultimately compile to asm ofc, unless they're managed code).

There's no "print" instruction since the CPU has nothing to do with that. That's part of the Operating System, so you use an OS function to write to the screen.
Post 18 Jan 2018, 20:26
View user's profile Send private message Reply with quote
Mino



Joined: 14 Jan 2018
Posts: 6

Thank you for the explanations and for the link.
I already knew that printf was the same as in C, and that the asm uses the system's dll for this task, but it's always good to call it back.

Indeed, I'm going to focus on learning, before thinking about optimizations, but it was simply out of curiosity Smile
Post 18 Jan 2018, 21:10
View user's profile Send private message Reply with quote
yeohhs



Joined: 19 Jan 2004
Posts: 100
Location: N 5.43564° E 100.3091°


Mino wrote:
I already knew that printf was the same as in C, and that the asm uses the system's dll for this task, but it's always good to call it back.



If you are only printing out plain strings, you can use puts, another C function. Smile

_________________
I'm an INTJ aspie.
Post 18 Jan 2018, 23:50
View user's profile Send private message Visit poster's website Reply with quote
Mino



Joined: 14 Jan 2018
Posts: 6

And what are the differences between puts and printf?
Post 20 Jan 2018, 10:25
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15383
Location: Monstropolis

printf will "format" the string with parameters.

Code:
ccall printf,"Value is %i",[some_value]
;output:
Value is 42

Whereas I assume puts simply prints the string without formatting.

Code:
ccall puts,"Value is %i"
;output:
Value is %i



Last edited by revolution on 20 Jan 2018, 10:42; edited 1 time in total
Post 20 Jan 2018, 10:34
View user's profile Send private message Visit poster's website Reply with quote
yeohhs



Joined: 19 Jan 2004
Posts: 100
Location: N 5.43564° E 100.3091°


Mino wrote:
And what are the differences between puts and printf?



There is an interesting discussion about this on stackoverflow.com. Smile

https://stackoverflow.com/questions/2454474/what-is-the-difference-between-printf-and-puts-in-c

_________________
I'm an INTJ aspie.
Post 20 Jan 2018, 10:37
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:  


< 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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2017, Tomasz Grysztar.