Results 1 to 8 of 8

Thread: ASM help?

  1. #1
    Member
    Join Date
    Jun 2013
    Location
    Sweden
    Posts
    150
    Thanks
    9
    Thanked 25 Times in 23 Posts

    ASM help?

    In the old days (80386) i could make small programs (.COM) like "read file, xor buffer with something, write to another file" but today I am totaly lost. I miss DEBUG.
    Can someone correct and make a executable Windows x64 of this code:

    mov rax,0
    loop:
    dec rax
    jne loop
    ret

    Just a simple loop!

  2. #2
    Member
    Join Date
    Jan 2014
    Location
    Bothell, Washington, USA
    Posts
    685
    Thanks
    153
    Thanked 177 Times in 105 Posts
    This is what you asked for, but may not be what you want.

    Code:
    main()
    {
      asm("movq $0, %rax");
      asm(".loop: subq $1, %rax");
      asm("cmpq $0, %rax");
      asm("jne .loop");
    }
    I used mingw-w64 version 4.9.1 to compile the code. It would take a very long time to complete since rax is 64 bits. Changing the subq $1 to subq $0x10000000 allows the program to run in about 40 seconds on my slower PC.
    Attached Files Attached Files
    Last edited by Kennon Conrad; 14th February 2015 at 21:19.

  3. The Following User Says Thank You to Kennon Conrad For This Useful Post:

    a902cd23 (14th February 2015)

  4. #3
    Member
    Join Date
    Jan 2014
    Location
    Bothell, Washington, USA
    Posts
    685
    Thanks
    153
    Thanked 177 Times in 105 Posts
    Similar code, but using only the 32 LSBs of RAX via EAX. I realized the cmp was not necessary.

    Code:
    main()
    {
      asm("movl $0, %eax");
      asm(".loop: subl $1, %eax");
      asm("jne .loop");
    }
    This one runs in about 2.5 seconds vs. 340 years for the RAX version.
    Attached Files Attached Files
    Last edited by Kennon Conrad; 14th February 2015 at 22:55.

  5. The Following User Says Thank You to Kennon Conrad For This Useful Post:

    a902cd23 (15th February 2015)

  6. #4
    Member just a worm's Avatar
    Join Date
    Aug 2013
    Location
    planet "earth"
    Posts
    96
    Thanks
    29
    Thanked 6 Times in 5 Posts
    If you are able to speak german then you could check the documentation archive of a german low level programming community. They show how to write an *.exe file (or an executable for linux) with a hex editor. They show it for a "hello world!"-programm but you can replace the instructions with what you need. But they only cover the 32 bit operation mode (protected mode).

    ftp://ontheserver.de
    Last edited by just a worm; 5th March 2015 at 10:18.

  7. #5
    Member just a worm's Avatar
    Join Date
    Aug 2013
    Location
    planet "earth"
    Posts
    96
    Thanks
    29
    Thanked 6 Times in 5 Posts
    here is a programm that does the following:
    Code:
    eax =xor eax
    ecx =xor ecx
    
    start:
    eax =- 1
    ecx =- cf
    
    jnz start
    
    return
    It will be bussy for 4 cycles * 2^64 on the most computers

    By the way: Compare the sizes of the exe files (1 kilobyte vs 45.4 kilobytes) :-D From this point it's understandable that some people keep the old days in mind.
    Attached Files Attached Files
    Last edited by just a worm; 5th March 2015 at 03:58.

  8. #6
    Member
    Join Date
    Jan 2014
    Location
    Bothell, Washington, USA
    Posts
    685
    Thanks
    153
    Thanked 177 Times in 105 Posts
    Quote Originally Posted by just a worm View Post
    here is a programm that does the following:
    Code:
    eax =xor eax
    ecx =xor ecx
    
    start:
    eax =- 1
    ecx =- cf
    
    jnz start
    
    return
    It will be bussy for 4 cycles * 2^64 on the most computers

    By the way: Compare the sizes of the exe files (1 kilobyte vs 45.4 kilobytes) :-D From this point it's understandable that some people keep the old days in mind.
    Your executable is not a 64-bit executable, which is what the a902cd23 requested. It will be slower since you are using two register and have three instructions in the loop instead of one register and two instructions. I did not strip out debugging information and did put the code in main for convenience, which causes a larger executable.

  9. #7
    Member just a worm's Avatar
    Join Date
    Aug 2013
    Location
    planet "earth"
    Posts
    96
    Thanks
    29
    Thanked 6 Times in 5 Posts
    Quote Originally Posted by Kennon Conrad
    Your executable is not a 64-bit executable, which is what the a902cd23 requested.
    Yes. Well, a902cd23 wasn't too precise wether the file should just run in Windows x64 or wether it should be according to the pe32+ file format (that is the 64 bit version of the file format). My version is according to the pe32 file format (that is the normal 32 bit version) but should run in Windows x64 even though only by the compatibility for 32 bit programms.

    Quote Originally Posted by Kennon Conrad
    I did not strip out debugging information and did put the code in main for convenience, which causes a larger executable.
    Since the question of the topic starter has basicly beeing answered I hope that noone considers my request to be "topic hi-jacking". Would you mind ... if you know ... to tell a little more about what makes the executable big? I can see that your compiler has inserted a lot of imports even though your code doesn't require any of them. Does a c-compiler automatically add something like a run-time environment? Your code is 7440 bytes long.
    Last edited by just a worm; 5th March 2015 at 10:34.

  10. #8
    Member
    Join Date
    Jan 2014
    Location
    Bothell, Washington, USA
    Posts
    685
    Thanks
    153
    Thanked 177 Times in 105 Posts
    Quote Originally Posted by just a worm View Post
    Yes. Well, a902cd23 wasn't too precise wether the file should just run in Windows x64 or wether it should be according to the pe32+ file format (that is the 64 bit version of the file format). My version is according to the pe32 file format (that is the normal 32 bit version) but should run in Windows x64 even though only by the compatibility for 32 bit programms.


    Since the question of the topic starter has basicly beeing answered I hope that noone considers my request to be "topic hi-jacking". Would you mind ... if you know ... to tell a little more about what makes the executable big? I can see that your compiler has inserted a lot of imports even though your code doesn't require any of them. Does a c-compiler automatically add something like a run-time environment? Your code is 7440 bytes long.
    I assumed he wanted 64-bit code because he had rax in his code, but you are right and were not off-topic.

    For this code compiled with gcc, it puts debug code in and also starting code that calls main. If I set flags to take those out and do an exit instead of main, it's 2K but crashes on completion. Usually I am not very concerned about executable size but it is surprising how much bloat gets added by default for a program with three lines of assembly code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •