Thread: ASM to C language

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    93

    ASM to C language

    I've been trying to translate this piece of Assembly code to C language, but I haven't been having much luck regarding the cmp's
    and theirs associated jumps (jg, jl, ja). I know there all associated with error handling and that's about it. Here's the ASM
    code with comments that I've added:

    push word ptr [09F0] //hFile
    push 0
    push 0
    push 2 //OF_READ
    call _llseek
    mov [bp-10], ax //LOWORD
    mov [bp-0E], dx //HIWORD
    mov [bp-FF6E], ax //int=LOWORD
    push word ptr [09F0] //hFile
    push 0
    push 0
    push 0
    mov si, ax
    call _llseek
    cmp word ptr [bp-0E], 00 //HIWORD
    jg 02A5
    jl 0292
    cmp word ptr [bp-10], -01 //LOWORD /* if (LOWORD(dword) == -1) ?? */
    ja 02A5
    push word ptr [0AAE] // hEdit
    lea ax, [si+01]
    push ax
    push 42
    call LocalReAlloc


    As far as translating to C I'm thinking something like this so far:

    Code:
    dword = (DWORD)_llseek(hFile, 0L, 2);
    int = LOWORD(dword);
    _llseek(hFile, 0L, 0);
    
    /* cmp's and jg, jl, ja */
    
    LocalReAlloc(hEdit, LOWORD(dword)+1, LHND);
    If anyone has any ideas on the translation of the cmp and the jg, jl, ja, statements to C it would be greatly appreciated.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    My assembly is not great, but I believe that the instructions means at least
    jg = jump greater
    jl = jump lower
    ja = jump ???
    The other problem is your hard-coded addresses. We don't know where the execution jumps when the specified conditions are true.
    Also, int is a reserved keyword, so you better rename that.

    Other stuff I can spot are:
    - I believe arguments are pushed in reverse order, so that the first argument can be popped off the stack first. You are sure that you haven't specified the arguments in the wrong order?
    - You are pushing 4 arguments for lseek. hFile, 0, 0, 2. Yet you are only calling it with 3?
    Last edited by Elysia; 06-27-2010 at 12:53 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    93
    One of the four arguments is hFile which is a Global Handle. It's paased from the OpenFile function prior to the ASM code I
    posted. The jump addr "02A5" for both jg and ja is a lstrcpy function. The jl jump addr "0290" is the LocalReAlloc
    function I posted.
    I just used "int" and "dword" as simple abreviations for the variables rather than specific variables such as: DWORD dwLen;
    or int iLen;. Sorry if it caused any confusion.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Location
    malaysia
    Posts
    1
    llseek in windows got 3 params

    _llseek Function (Windows)
    LONG _llseek(
    HFILE hFile, // handle to file
    LONG lOffset, // number of bytes to move
    int iOrigin // starting position
    );
    but in your example code.
    Code:
    push word ptr [09F0] //hFile
    push 0
    push 0
    push 2 //OF_READ
    call _llseek
    and
    Code:
    push word ptr [09F0] //hFile
    push 0
    push 0
    push 0
    mov si, ax
    call _llseek
    the code did 4 push (equal to 4 params) if i am not mistaken.

    but linux llseek
    int _llseek(unsigned int fd, unsigned long offset_high, unsigned long offset_low, loff_t *result, unsigned int whence);
    seems to have 5 params.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    93
    The "mov si, ax" instruction appears to be something for setting up (or related to) the up coming jg, jl, ja, and cmp calls, and
    and not actually a parameter of the one _llseek call. For example, if I compile this code it will place a "mov si, ax" in the same
    location (in the one _llseek call) as the ASM code I originally posted.

    Code:
    dwLen = (DWORD)_llseek (hFile, 0L, 2);
    iLen  = LOWORD(dwLen);
    _llseek (hFile, 0L, 0);
    
    if (HIWORD(dwLen) == 0 && LOWORD(dwLen) == -1)
       /* do something */

  6. #6
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    ja = jump ???
    ja="jump if greater: unsigned". I have no idea how it got the mnemonic ja but that's what it is.

    and
    [/CODE]
    push word ptr [09F0] //hFile
    push 0
    push 0
    push 0
    mov si, ax
    call _llseek
    [/CODE]
    the code did 4 push (equal to 4 params) if i am not mistaken.
    Well... the second argument is a long, so it has to push 2 16-bit ints to fill that space.

    - I believe arguments are pushed in reverse order, so that the first argument can be popped off the stack first. You are sure that you haven't specified the arguments in the wrong order?
    That is true but the C functions are definitely in the correct order.

    mov si, ax
    ...
    lea ax, [si+01]
    SI is a pointer-register; [si+01] is equivalent to *(&ax + 1) I believe. So that would be accessing HIWORD(dword).

    And as for the "cmp"s, they don't branch themselves, they set flags. The ja, jg, jl branch based on the flags set from the previous cmp. So
    cmp word ptr [bp-0E], 00 //HIWORD
    jg 02A5
    jl 0292
    would be like
    Code:
    if (HIWORD(dword) > 0)
        //jump to 02A5
    else if (HIWORD(dword) < 0)
        //jump to 0292
    Where I assume that 02A5 and 0292 are addresses where the code inside the if brackets reside. I don't think they're the strcopy and realloc functions; functions are called and not jumped to. The operands 02A5 and 0292 mean to jump relative to the address of the actual jump instruction, on a near jump.
    Last edited by bernt; 06-28-2010 at 08:54 AM.
    Consider this post signed

  7. #7
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Quote Originally Posted by bernt View Post
    ja="jump if greater: unsigned". I have no idea how it got the mnemonic ja but that's what it is.

    Jump if Above

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Opcode ja means to jump to the specified address if CF and ZF flags are zero.
    From the posted code it looks like LocalReAlloc() takes four arguments, as in
    Code:
    push word ptr [0AAE] // hEdit
    lea ax, [si+01]
    push ax
    push 42
    call LocalReAlloc

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    93
    Appearently, "(HIWORD(dword) < 0)" cannot be less than zero since the conditional expression is constant, so the compile removes
    expression from the source code.

    If I take this code and compile it I end up with this ASM as the result:

    Code:
    dwLen = (DWORD)_llseek (hFile, 0L, 2);
    iLen  = LOWORD(dwLen);
    _llseek (hFile, 0L, 0);
    
    if (HIWORD(dwLen) > 0)
        goto blah;
    else if (HIWORD(dwLen) > 0 && LOWORD(dwLen) == -1)
        goto blah;

    ASM result:

    Code:
    push word ptr [09F0] //hFile
    push 0
    push 0
    push 2 //OF_READ
    call _llseek
    mov [bp-10], ax //LOWORD
    mov [bp-0E], dx //HIWORD
    mov [bp-FF6E], ax //int=LOWORD
    push word ptr [09F0] //hFile
    push 0
    push 0
    push 0
    call _llseek
    cmp word ptr [bp-0E], 00 //HIWORD
    jnz 02A5
    jz 0292
    cmp word ptr [bp-10], -01 //LOWORD /* if (LOWORD(dword) == -1) ?? */
    jz 02A5
    push word ptr [0AAE] // hEdit
    lea ax, [si+01]
    push ax
    push 42
    call LocalReAlloc
    As you can see the "mov si, ax" statement is not present in the one _llseek call, and the jumps are jnz, jz, jz, verses the jg,
    jl, ja in the original ASM code. BTW, I left the LocalReAlloc funtion out of the C source code I posted.

  10. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    93
    push word ptr [0AAE] // hEdit
    lea ax, [si+01]
    push ax
    push 42
    call LocalReAlloc
    Doesn't the term "lea" refer to a memory location?

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by TAZIN View Post
    Doesn't the term "lea" refer to a memory location?
    Yep! it loads the effective address of si+01 into the register ax.
    In C that's akin to passing a pointer instead of the actual value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. d programming language - a better c++?
    By kypronite in forum Tech Board
    Replies: 12
    Last Post: 02-28-2011, 02:55 AM
  2. The value of learning a new programming language
    By h3ro in forum General Discussions
    Replies: 21
    Last Post: 06-13-2009, 01:48 AM
  3. ASM to C language
    By TAZIN in forum C Programming
    Replies: 22
    Last Post: 06-03-2009, 06:29 AM
  4. How to use asm code in c language
    By phijo in forum C Programming
    Replies: 5
    Last Post: 05-08-2004, 02:58 AM
  5. Wanna learn Assembly?
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 137
    Last Post: 06-29-2002, 01:49 AM