flat assembler
Message board for the users of flat assembler.
Index
> Main > How to push/pop a float |
Author |
|
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? |
|||
28 Mar 2020, 19:29 |
|
revolution 28 Mar 2020, 19:30
Code: push 3.14159 mov eax,2.718281828 |
|||
28 Mar 2020, 19:30 |
|
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? |
|||
28 Mar 2020, 21:07 |
|
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? 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 |
|||
28 Mar 2020, 21:31 |
|
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? |
|||
29 Mar 2020, 07:11 |
|
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 |
|||
29 Mar 2020, 07:35 |
|
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). |
|||
29 Mar 2020, 10:24 |
|
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. |
|||
29 Mar 2020, 18:18 |
|
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 |
|||
29 Mar 2020, 18:42 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.