flat assembler
Message board for the users of flat assembler.

Index > Windows > Fasm and Mysql

Author
Thread Post new topic Reply to topic
clamicun



Joined: 04 Dec 2013
Posts: 77
Location: Muenster NRW Germany
clamicun 02 Nov 2015, 21:20
;Since years I use a php program (Xampp and mysql) to organize my bankaccounts.
;Now - just for fun I am trying to write the same prog in Assembler.

;Many things are still 'nebulous', but this one is essential...

;How to deal with floats in general ?

;The iput 'value' comes from a dialogbox (dialogitem EDIT).
;it is a string, that somehow must be converted into a float ??

;How to put a float (double) into the database with fasm ? (MY EXAMPLE)

;=====================

format PE GUI 4.0
entry start

include '..\..\include\win32ax.inc' ;You probably have to change the path

section ".data" data readable writeable

Buffer db "60 dup(?)",0
result db "%s, %li",0

insertTest1 db "INSERT INTO bank_table VALUES(%s,%li)",0 ;For mysql
action1 db "Transaction",0 ;String in mysql
value1 dd ? ;mysql value: 'value1' double(11,2) DEFAULT NULL

insertTest2 db "INSERT INTO bank_table VALUES(%s,%li.%li)",0 ;For mysql
valueTest1 dd ?
valueTest2 dd ?

;=====================
section ".code" code readable executable
start:

mov [value1],-45 ;ok, but bankaccounts deal mostly with floats
cinvoke wsprintf,Buffer,result,action1,[value1]
invoke MessageBox,0,Buffer,"result - ok",0

;Does not work naturally. How to do this ? I suppose value must be of a different type,if at all.
mov [value1],-45.33
cinvoke wsprintf,Buffer,insertTest1,action1,[value1]
invoke MessageBox,0,Buffer,"insertTest1 - strange result",0

;Of course it can be done this way, but it is no real solution.
mov [valueTest1],-45
mov [valueTest2],33
cinvoke wsprintf,Buffer,insertTest2,action1,[valueTest1],[valueTest2]
invoke MessageBox,0,Buffer,"insertTest2 - ok",0

invoke ExitProcess,0

;=====================
section ".idata" import data readable writeable

library kernel32, "kernel32.dll",\
user32,"user32.dll"

include "..\..\include\api\kernel32.inc" ;Path !
include "..\..\include\api\user32.inc"
Post 02 Nov 2015, 21:20
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20333
Location: In your JS exploiting you and your system
revolution 03 Nov 2015, 00:23
So if I read the correctly, all you need is a way to print floats into ASCII text? If so the import msvcrt.dll and use the sprintf (or equivalent) function.
Post 03 Nov 2015, 00:23
View user's profile Send private message Visit poster's website Reply with quote
ProphetOfDoom



Joined: 08 Aug 2008
Posts: 120
Location: UK
ProphetOfDoom 03 Nov 2015, 11:14
Clamicun,

I don't know the answer to your question, but in any case it's not a good idea to use floats or doubles to represent currency. This stack overflow answer explains why (basically, currency requires exactness, which floats don't provide):

http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency
Post 03 Nov 2015, 11:14
View user's profile Send private message Reply with quote
clamicun



Joined: 04 Dec 2013
Posts: 77
Location: Muenster NRW Germany
clamicun 03 Nov 2015, 14:11
revolution thank you,
well - what I want to do first is convert a string from a dialogbox input into a float.

I know that there is a function atof in msvcrt.
But where is msvcrt ?
My fasmversion - surprisingly - does not have it.

So I am using msvcrt.inc from MASM. Should be same ??

teststr db "12.53",0
(c)invoke atof,teststr ;is this the correct syntax ?

The result should be where - eax ?

Does not work !

What is this locale business ? My german system uses , insteadof .

If you please, write an example, which works.
Post 03 Nov 2015, 14:11
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 03 Nov 2015, 15:16
clamicun
If you aren't after implementing the db communication protocol from scratch, you're gonna need a library such as this one. And when you have your choice met, then the library defines for you what format it needs the numbers to be passed (see here for libsqlclient).

I wouldn't suggest you to use msvcrt, because a library as the one above might not need the numbers in a textual representation. You'd just reference your number in a bind structure before calling mysql_stmt_bind_param . The way you stored the number with mov [value1],-45.33 is perfectly valid for single precision floatings.

Quote:
So I am using msvcrt.inc from MASM. Should be same ??

msvcrt.inc from masm has nothing to do with the way fasm declares imports. Just specify the functions you need with the library and import macros the same way you did it in the first post. You don't need a separate inc-file for that.

Quote:
teststr db "12.53",0
(c)invoke atof,teststr ;is this the correct syntax ?

You initially wanted to convert floats to string. What does atof have to do with that? As revolution suggested you could use sprintf .

_________________
Faith is a superposition of knowledge and fallacy
Post 03 Nov 2015, 15:16
View user's profile Send private message Reply with quote
clamicun



Joined: 04 Dec 2013
Posts: 77
Location: Muenster NRW Germany
clamicun 03 Nov 2015, 15:32
L_inc,
everybody is not going to understand me - why ?
Are we talking a different language ?

I did NOT want initially to convert floats to string !!

This is what I wrote above...beginning of this page...

"The input 'value' comes from a dialogbox (dialogitem EDIT).
it is a string, that somehow must be converted into a float ??"
Post 03 Nov 2015, 15:32
View user's profile Send private message Reply with quote
clamicun



Joined: 04 Dec 2013
Posts: 77
Location: Muenster NRW Germany
clamicun 03 Nov 2015, 15:39
P.S. to L_inc

What are you talking ? I did not mention mysql problems.
I am talking on Fasm instead of php and mysql.
I am writing websites since 2001
Post 03 Nov 2015, 15:39
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 03 Nov 2015, 15:56
clamicun
Well, sorry I misinterpreted what was your primary objective. All your code samples you've shown you had problems with were related to wsprintf . Hence the misunderstanding.

Quote:
teststr db "12.53",0
(c)invoke atof,teststr ;is this the correct syntax ?

Yes. It is. You need cinvoke .

Quote:
The result should be where - eax ?

The result should be in st0 , which is then possible to extract into a memory location using fstp qword[buffer] .

Quote:
What is this locale business ? My german system uses , insteadof .

Yes, so what's the problem?

Quote:
What are you talking ? I did not mention mysql problems.
I am talking on Fasm instead of php and mysql.

I though, you were talking about replacing php with asm. In order to do that you need an interface library for your mysql db.

_________________
Faith is a superposition of knowledge and fallacy
Post 03 Nov 2015, 15:56
View user's profile Send private message Reply with quote
clamicun



Joined: 04 Dec 2013
Posts: 77
Location: Muenster NRW Germany
clamicun 03 Nov 2015, 16:23
L_inc thank you,
I wrote (c)invoke because I tested the two possibilities. None worked.

What do you mean " st0 " ? Is it the below buffer ?

My error was that I thought the result would be in eax ?

Then...
fstp qword[buffer] ???

The locale business is important.
Try getting 12,44 into an american database.

P.S.
php is much easier
Post 03 Nov 2015, 16:23
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 03 Nov 2015, 17:13
clamicun
Quote:
What do you mean " st0 " ? Is it the below buffer ?

st0 is one of the eight FPU (floating point unit, part of the CPU) registers logically organized in a stack (not to confuse with the program stack). If you want to deal with floating point numbers it's absolutely necessary for you to find a tutorial on the x87 FPU programming and to use the Intel Manuals as a reference.

Quote:
Then...
fstp qword[buffer] ???

Please try to compose your questions more complete. fstp is an FPU instruction that pops a value from the FPU stack.

Quote:
The locale business is important.
Try getting 12,44 into an american database.

I know it is. But you didn't describe your problem. Try to be more specific in your questions, if you don't like misunderstandings. What are you trying to achieve? Use arbitrary locales? You can use then setlocale or _create_locale / _free_locale with LC_NUMERIC .

_________________
Faith is a superposition of knowledge and fallacy
Post 03 Nov 2015, 17:13
View user's profile Send private message Reply with quote
clamicun



Joined: 04 Dec 2013
Posts: 77
Location: Muenster NRW Germany
clamicun 03 Nov 2015, 19:49
Ok L_inc,
thank you a lot.
But....

Do not try to "educate" people in this bloody arrogant way !
(Are you from the US ?)

"What do you mean "st0 " ... is a perfectly complete question "

Your answer:

"st0 is one of the eight FPU (floati.... " ... is a perfectly clear answer.

But spare me things like:
"Please try to compose your questions more complete"
"Try to be more specific in your questions"

Good evening clamicun
Post 03 Nov 2015, 19:49
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc 03 Nov 2015, 20:46
clamicun
Nein, ich bin sehr bescheiden und komme aus dem gleichen Staat wie Sie. Smile Was Ihre Fragen und meine Belehrungen angeht, da, wo ich problemlos antworten konnte, habe ich auch nicht kommentiert. Wo Sie aber nur sinnlose Fragezeichen gesetzt haben, ohne sich die Mühe zu geben, die Frage sinnvoll zu formulieren, musste ich nur raten, worum es geht. Und möglicherweise auch falsch. Im Zusammenhang von ausschließlich string to float Umrechnung macht zum Beispiel die Diskussion über konfigurierbare Lokale keinen Sinn, da die Zahlen an die Datenbank im lokalunabhängigen Format weitergereicht werden.
Quote:
But spare me things like:
"Please try to compose your questions more complete"
"Try to be more specific in your questions"

Durch Ihre Frage: "Are we talking a different language?" — haben Sie selber gezeigt, dass Sie die Erklarung für Missverständnisse brauchen.

_________________
Faith is a superposition of knowledge and fallacy
Post 03 Nov 2015, 20:46
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 05 Nov 2015, 22:26
Don't use floating numbers for saving currency. Instead convert them to the lowest unit of your currency.

For instance US $24.75 * 100 = 2475 Cents. Store 2475 into the database as long.

Then when querying you can use a function to get the integer and floating part.

Code:
void getCurrency(unsigned long value, int * dollars, int * cents)
{
  *dollars = value / 100;
  *cents = value MOD 100;
}

long money = 2475;
int dollars, cents;
wchar_t pszMoney[100] = {0};

getCurrency(money, &dollars, &cents);
wsprintfW(pszMoney, "$ %d.%.02d", dollars, cents);
SetWindowTextW(hwndTextMoney, pszMoney);
    
Post 05 Nov 2015, 22:26
View user's profile Send private message 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


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

Website powered by rwasa.