flat assembler
Message board for the users of flat assembler.

Index > Main > How to push/pop a float

Author
Thread Post new topic Reply to topic
rc



Joined: 03 Nov 2019
Posts: 55
Location: Germany
rc 28 Mar 2020, 19:29
Hello,

how do i push a float onto the stack and pop it into a register, or mov a float into a register?
Post 28 Mar 2020, 19:29
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 28 Mar 2020, 19:30
Code:
push 3.14159
mov eax,2.718281828    
Post 28 Mar 2020, 19:30
View user's profile Send private message Visit poster's website Reply with quote
rc



Joined: 03 Nov 2019
Posts: 55
Location: Germany
rc 28 Mar 2020, 21:07
How do i know when i pop it from the stack if it was a float or not?
Code:
push 3.14159
pop eax ; is it a float or int?    
Post 28 Mar 2020, 21:07
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 28 Mar 2020, 21:20
It’s you who should have pushed it onto the stack before that, so you do know.
rc wrote:
How do i know when i pop it from the stack if it was a float or not?
Code:
push 3.14159
pop eax ; is it a float or int?    
It’s a set of 32 bits. The one that was pushed (equivalent to the float representation of the number).
Post 28 Mar 2020, 21:20
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: 20445
Location: In your JS exploiting you and your system
revolution 28 Mar 2020, 21:31
rc wrote:
How do i know when i pop it from the stack if it was a float or not?
Just to add to the previous reply.

It could be anything. These are all equivalent:
Code:
dd 3.14159 ;byte stream: 0xd0, 0x0f, 0x49, 0x40
db 0xd0, 0x0f, 0x49, 0x40 ;float 3.14159
dd 0x40490fd0 ;float 3.14159
dd 1078530000 ;float 3.14159    
Post 28 Mar 2020, 21:31
View user's profile Send private message Visit poster's website Reply with quote
rc



Joined: 03 Nov 2019
Posts: 55
Location: Germany
rc 29 Mar 2020, 07:11
I am wondering where the information of the position of the decimal point is stored?

To learn assembly i try to write a simplistic translator that translate source code of an imaginary language to assembly. (Only int and float are supported 32bit)
And i have a hard time to find out if a number is an int or float, since i don't know what it was.

I have the following case:
Code:
# Imaginary language
let foo = 10
let bar = 2.5
push foo
push bar    


Fasm:
Code:
mov eax, 10
push eax
mov eax, 2.5
push eax    


The equivalent fasm code pushes foo and bar onto the stack, know i need to find out what data type it was. Let's say the user want to print these two variables:
Code:
# Imaginary language
pop val1
pop val2
print val1
print val2

# Output should be:
> 2.5
> 10    


Is there no way to find out what representation it was in fasm? (like in c the typeof operator)
If not, how would one go and keep track of the types?
Post 29 Mar 2020, 07:11
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 29 Mar 2020, 07:35
fasm has no native type information. The CPU has no type information. You have to track it yourself.

One way might be to store an auxiliary value stating the type:
Code:
FLOAT = 1
INTEGER = 2

value1 dd 2.5, FLOAT
value2 dd 13, INTEGER    
Post 29 Mar 2020, 07:35
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 29 Mar 2020, 10:24
[Just to add to the previous reply.]
… but storing type information basically transfers your project to reinventing and using Variant type which is not too bad but is definitely not as effective as native types.

At the lowest level the type information is implied by the operations applied to data. It might also be stored by the compiler side-by-side with the program (debug info and stuff) but is not used by the program itself (unless one uses reflection, in which case there’s much more trouble than just typing).
Post 29 Mar 2020, 10:24
View user's profile Send private message Visit poster's website Reply with quote
donn



Joined: 05 Mar 2010
Posts: 321
donn 29 Mar 2020, 18:18
Quote:
I am wondering where the information of the position of the decimal point is stored?


If you want to go deep into the format of floating point numbers, some links are in here:

IEEE 754

As previously mentioned, 'typing' would have to be maintained by you. There are some convenience functions to convert between integers and float once you have them such as cvtsi2ss and you can use library functions (heavything may have some? C libs may) if you want to extract just the integer part or fractional part of the number, or build your own. I can't remember if a native x86 instruction can extract the position of the decimal place, but I think if you cvtss2si you can get the integer part, subtract that from the number, etc.
Post 29 Mar 2020, 18:18
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 29 Mar 2020, 18:42
For 32-bit floats the exponent is in bits 30..23:
Code:
mov eax, 3.14159
and eax, 0xff shl 23 ; isolate the exponent
shr eax, 23 ; get the exponent + bias
sub eax, 0x7f ; eax = the binary point position

x = dword 3.14159
e1 = x and (0xff shl 23) ; isolate the exponent
e2 = e1 shr 23 ; get the exponent + bias
e3 = e2 - 127 ; e3 = the binary point position    
Post 29 Mar 2020, 18:42
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


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

Website powered by rwasa.