flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Macro EQU Float to Integer?

Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 1796
Roman 25 Sep 2014, 15:10
I have Num1 EQU 20.0
How get NumInt = Integer(Num1) ? Then NumInt = 20
I mean not using CPU commands , only use Fasm macro.
Fasm can do this ?
Post 25 Sep 2014, 15:10
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20344
Location: In your JS exploiting you and your system
revolution 25 Sep 2014, 15:54
Yes, fasm can do it but it ain't pretty.

One method would be to store the number as a float and then decode the float value to extract the integer.
Code:
virtual
  temp_float dq Num1
  load float_binary qword from $$
  ;code goes here to decode the IEEE754 float format
end virtual    
Post 25 Sep 2014, 15:54
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8354
Location: Kraków, Poland
Tomasz Grysztar 25 Sep 2014, 17:22
revolution wrote:
One method would be to store the number as a float and then decode the float value to extract the integer.
Code:
virtual
  temp_float dq Num1
  load float_binary qword from $$
  ;code goes here to decode the IEEE754 float format
end virtual    
This code is in fact equivalent to a simple:
Code:
float_binary = qword Num1
;code goes here to decode the IEEE754 float format    
Post 25 Sep 2014, 17:22
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1796
Roman 25 Sep 2014, 17:22
revolution how using you code ?
You code giv EQU value or label of memory ?
Please show how you code work.
Thanks for you help.
Post 25 Sep 2014, 17:22
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1796
Roman 25 Sep 2014, 17:26
For example i want do this.
Num1 EQU 20.0
mov eax,Integer(Num1)
And eax= 20
Post 25 Sep 2014, 17:26
View user's profile Send private message Reply with quote
RIxRIpt



Joined: 18 Apr 2013
Posts: 50
RIxRIpt 25 Sep 2014, 19:14
I found this problem interesting, so I decided to give a try: Smile
Roman wrote:
For example i want do this.
Num1 EQU 20.0
mov eax,Integer(Num1)
And eax= 20

afaik, fasm macros cannot return a value, as an alternative you can store it in a global variable:
Code:
;Display macros, for numbers
;sorry, idk who made these macros first
;found & taken from: http://wasm.ru/forum/viewtopic.php?pid=520153 (from l_inc)

;Displays a binary representation of a number
macro dispBin num*, padding, leader, trailer, size
{
        local digCount,number,lastdig
        number = size num
        lastdig = number

        if number < 0
                display '-'
                number = -(number shr 1 + number and 1)
                lastdig = -(lastdig + number + number)
        else
                number = number shr 1
                lastdig = lastdig and 1
        end if
        digCount = 0
        while number shr digCount > 0
                digCount = digCount + 1
        end while
        if ~ leader eq
                display leader
        end if
        if ~ padding eq
                if digCount < padding
                        times (padding-digCount-1) display '0'
                end if
        end if
        times digCount display number shr (digCount-%) and 1+'0'
        display lastdig+'0'
        if ~ trailer eq
                display trailer
        end if
}

;Displays a decimal representation of a number
macro dispDec num*, padding, leader, trailer
{
        local digCount,tenPow,number,lastdig
        number = num
        lastdig = number
        
        if number < 0
                display '-'
                number = (-(number shr 1 + number and 1)) / 5
                lastdig = -(lastdig + number*5 + number*5)
        else
                number = number/10
                lastdig = lastdig mod 10
        end if
        digCount = 0
        tenPow = 1
        while tenPow <= number
                tenPow = tenPow*10
                digCount = digCount + 1
        end while
        if ~ leader eq
                display leader
        end if
        if ~ padding eq
                if digCount < padding
                        times (padding-digCount-1) display '0'
                end if
        end if
        repeat digCount
                tenPow = tenPow/10
                display number/tenPow+'0'
                number = number mod tenPow
        end repeat
        display lastdig+'0'
        if ~ trailer eq
                display trailer
        end if
}

;Displays a hexadecimal representation of a number
macro dispHex num*, padding, leader, trailer, size
{
        local digCount,dig,number,lastdig
        number = size num
        lastdig = number
        
        if number < 0
                display '-'
                number = (-(number shr 1 + number and 1)) shr 3
                lastdig = -(lastdig + number shl 3 + number shl 3)
        else
                number = number shr 4
                lastdig = lastdig and 0xF
        end if
        digCount = 0
        while number shr (digCount*4) > 0
                digCount = digCount + 1
        end while
        if ~ leader eq
                display leader
        end if
        if ~ padding eq
                if digCount < padding
                        times (padding-digCount-1) display '0'
                end if
        end if
        repeat digCount
                dig = number shr ((digCount-%)*4) and 0Fh
                if dig > 9
                        dig = dig-'0'+('A'-10)
                end if
                display dig+'0'
        end repeat
        if lastdig > 9
                lastdig = lastdig-'0'+('A'-10)
        end if
        display lastdig+'0'
        if ~ trailer eq
                display trailer
        end if
}

;Displays a string with binary, decimal and hexadecimal values
;usage: display d=number,":",9,b=number,9,h=number,13,10
macro display [token]
{
        forward
                define matched +
                define matched -
                irp type, =b, =d, =h, =bb, =bw, =bd, =bq, =hb, =hw, =hd, =hq \{ match type==n,token \\{ restore matched \\} \}
                match =b==number,token \{ dispBin number,,,'b' \}
                match =d==number,token \{ dispDec number \}
                match =h==number,token \{ dispHex number,,'0x' \}
                match =bb==number,token \{ dispBin number,,,'b',byte \}
                match =bw==number,token \{ dispBin number,,,'b',word \}
                match =bd==number,token \{ dispBin number,,,'b',dword \}
                match =bq==number,token \{ dispBin number,,,'b',qword \}
                match =hb==number,token \{ dispHex number,,'0x',,byte \}
                match =hw==number,token \{ dispHex number,,'0x',,word \}
                match =hd==number,token \{ dispHex number,,'0x',,dword \}
                match =hq==number,token \{ dispHex number,,'0x',,qword \}
                match -,matched \{ display token \}
                match -,matched \{ restore matched \}
                restore matched
}


;My macro for Roman Smile
IEEE754_Result = 0
macro IEEE754 num_in, bit {
        local mantissa_size, exp_size, sign
        
        IEEE754_Result = 0
        
        if bit = 32
                mantissa_size = 23
                exp_size = 8
                num = dword num_in
        else if bit = 64
                mantissa_size = 52
                exp_size = 11
                num = qword num_in
        else
                display 'Unsupported size of IEEE754 number!', 10
                err
        end if
        
        exp = (num shr mantissa_size) and ((1 shl exp_size) - 1)
        sign = num shr (bit - 1)
        
        if bit = 32
                exp = exp - 127
        else if bit = 64
                exp = exp - 1023
        end if
        
        if exp >= 0
                IEEE754_Result = 1 shl exp
                repeat mantissa_size
                        exp = exp - 1
                        display 'i: ', d=%
                        m = (num shr (mantissa_size - %)) and 1
                        display '   m: ', d=m, '       ', d=m shl exp, 13, 10
                        IEEE754_Result = IEEE754_Result + (m shl exp)
                        if exp = 0
                                break
                        end if
                end repeat
                if sign
                        IEEE754_Result = -IEEE754_Result
                end if
        else
                IEEE754_Result = 0
        end if
}

IEEE754 -58723597273895.0, 64
display h=qword -58723597273895.0, 13, 10
display d=IEEE754_Result, 13, 10
    


Update: a better version without repeat macro
Code:
IEEE754_Result = 0
macro IEEE754 num_in, bit {
        local mantissa_size, exp_size
        IEEE754_Result = 0
        if bit = 32
                mantissa_size = 23
                exp_size = 8
                num = dword num_in
        else if bit = 64
                mantissa_size = 52
                exp_size = 11
                num = qword num_in
        else
                display 'Unsupported size of IEEE754 number!', 10
                err
        end if
        exp = ((num shr mantissa_size) and ((1 shl exp_size) - 1)) - (1 shl (exp_size - 1)) + 1
        if exp >= 0
                IEEE754_Result = (1 shl exp) or ((num shr (mantissa_size - exp)) and ((1 shl exp) - 1))
                if num shr (bit - 1) 
                        IEEE754_Result = -IEEE754_Result
                end if
        end if
}
    

_________________
Привет =3
Admins, please activate my account "RIscRIpt"
Post 25 Sep 2014, 19:14
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.