简体   繁体   中英

how to call a function in assembly?

How would I go about designating a portion of my assembly code as a function and calling that function from within the _asm {} block? Let's say for example, I wanted the function to start at the for loop, "n" elements are filled with a value.

#include <stdio.h>
#include <string.h>


void printBytes(char *data, int length)
{
    int x;
    for (x = 0; x < length; x++)
    {
        if ((x & 0xF) == 0) printf("\n");
        printf("%02X ", (unsigned char)data[x]);
    }
    printf("\n\n");
    return;
} // printBytes

void function(){

    char string[] = "The end is near!";
    void* dst = &string;
    unsigned char value = 0xA5;
    int nCount = 5;

    printf("The message is: %s\n", string);
    printBytes(string, strlen(string)); 

    __asm {

        //type cast the str from void* to char*
            mov eax, DWORD PTR [dst]
            mov DWORD PTR [ebp-88], eax

        //fill "n" elements/blocks with value
            mov DWORD PTR [ebp-76], 0
            jmp label_3
        label_2:
            mov eax, DWORD PTR[ebp-76]
            add eax, 1
            mov DWORD PTR [ebp-76], eax
        label_3:
            mov eax, DWORD PTR [ebp-76]
            cmp eax, DWORD PTR [nCount]
            jge EXIT

            mov eax, DWORD PTR [ebp-88]
            add eax, DWORD PTR [ebp-76]
            mov cl, BYTE PTR [value]
            mov BYTE PTR[eax], cl
            jmp label_2
        EXIT:

    }// asm_memset */

    printf("Now the message is: %s\n", string);
    printBytes(string, strlen(string));
    return;

}

int main() {

    function();

    printf("Press ENTER key to continue...");
    getchar();
    return(0);
}

Please note the solution to your problem is likely to be non-portable.

You will need to find the ABI for your architecture/system/compiler, and comply with its calling conventions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM