flat assembler
Message board for the users of flat assembler.

Index > Main > Can't move 64 bit immediate in 32 bit program.

Author
Thread Post new topic Reply to topic
GuBar



Joined: 05 Aug 2020
Posts: 4
GuBar 05 Aug 2020, 22:55
I am new to assembly (only started a few months ago) and I have grasped some concepts already. Now I am trying to learn how to use the x87 instruction set. I can't move a 64 bit immediate to a 64 bit memory location.

Code:
section '.data' data readable writeable
float dq 1
section '.text' code readable executable

_start:         ; a 32 bit program that uses 64 bit floating point values

        push ebp
        mov ebp, esp
        mov [float], 0x4029000000000000 ;this value is 1.25 and is where i get the error "illegal instruction"
        fld qword [float]
        mov [float], 0x3FFA000000000000 ;this value is 1.625 and i suspect that i will get an "illegal instruction" error here too
        fld qword [float]
        faddp           ;adds 1.25 and 1.625 together
        fst [float]     ;stores the sum in [float]
        push [float]    ;i also get an error here where i can't push a 64 bit value
        push fmt
        call [printf]   ;i am expecting to get 0x4007000000000000 which is 2.875
        add esp, 8

        ret 0
    


Description: floatcalculator is the 32 bit program with 64 bit floats
floatcalculator32 is the 32 bit program with 32 bit floats

Download
Filename: sources.zip
Filesize: 1.17 KB
Downloaded: 533 Time(s)

Post 05 Aug 2020, 22:55
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20582
Location: In your JS exploiting you and your system
revolution 06 Aug 2020, 03:04
fasm can convert the values for you. To store values larger than 32-bits you need to break them down into smaller portions:
Code:
address dq 1
my_num = qword 1.2345 ;64-bit value
mov dword[address+0],my_num and 0xffffffff ;lower 32 bits
mov dword[address+4],my_num shr 32 ;upper 32 bits    
Post 06 Aug 2020, 03:04
View user's profile Send private message Visit poster's website Reply with quote
GuBar



Joined: 05 Aug 2020
Posts: 4
GuBar 06 Aug 2020, 05:57
Thanks for your reply, the snippet of code you have shown me has solved my issue, but I have another issue. As I can't push a quad word onto the stack, I have pushed the lower and upper 32 bit values into the stack for the printf function. I remember learning that you pass arguments via the stack in reverse order(for printf you push the value you want to print then the format). so in order to print it correctly I do this,
Code:
        push dword [float+4]
        push dword [float+0]
        push fmt
        call [printf]   ;this is suppose to print out 0x0x4007000000000000 but instead prints out 0x0000000000000000
    


but when I do this
Code:
push dword [float+0]
        push dword [float+4]    ;this would print out the hex value in reverse
        push fmt
        call [printf]   ;oddly it prints out as 0x0000000040070000 as it should but the other program only printed out 0's
    

The only potential solution I could come up with is to call printf two times passing the upper bits with the format that includes "0x" at the beginning then the lower bits with a special format that excludes "0x". I am curious why it will print the correct hex number when I pass [float+0] and then [float+4] but not in the reverse order.


Description:
Filesize: 9.69 KB
Viewed: 5881 Time(s)

fasm.png


Post 06 Aug 2020, 05:57
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20582
Location: In your JS exploiting you and your system
revolution 06 Aug 2020, 06:09
Show your fmt string.
Post 06 Aug 2020, 06:09
View user's profile Send private message Visit poster's website Reply with quote
GuBar



Joined: 05 Aug 2020
Posts: 4
GuBar 06 Aug 2020, 07:44
this was the format string
Code:
fmt: db "0x%16x", 0     ;this is the format to print in 16 hex hex digits    
Post 06 Aug 2020, 07:44
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20582
Location: In your JS exploiting you and your system
revolution 06 Aug 2020, 07:50
You need to show two hex values because the 32-bit printf can only show 32-bit values AFAIAA.

Try this:
Code:
fmt: db "0x%08x%08x", 0    
Post 06 Aug 2020, 07:50
View user's profile Send private message Visit poster's website Reply with quote
GuBar



Joined: 05 Aug 2020
Posts: 4
GuBar 06 Aug 2020, 08:11
Thanks for the assistance, I will try this in a few hours as I have to sleep.
Post 06 Aug 2020, 08:11
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.