在 Windows x64 程序集中,如何访问函数的 8 字节整数参数?

问题描述 投票:0回答:1

我正在开发一个将从 C 程序调用的汇编函数。

我的设置:

  1. x64 CPU
  2. Windows 11
  3. NASM(依赖于 Intel 语法)
  4. nasm -f win64

我的问题是:假设该函数只需要一个(8 字节)参数,我如何根据

RBP
寄存器找到它?

迄今为止我最好的尝试:

global _mypopcnt
section .text
            
_mypopcnt:  push rbp            ; Prolog starts here.
            mov rbp, rsp        ; Prolog ends here.
            push r8             ; Save rbx.
            mov r8, [rbp + 8]   ; Read the value to bit count.
            popcnt rax, r8      ; Count bits. 
            pop rbx             ; Restore rbx.
            mov rsp, rbp        ; Epilog starts here.
            pop rbp             ; Epilog ends here.
            ret                 ; Done!

C 程序如下所示:

#include <stdio.h>

int _mypopcnt(unsigned long long value);

int main() {
    int num = _mypopcnt(0xffffffff);
    printf("%d\n", num);
    return 0;
}

我的(MinGW)Makefile 是:

all: main.obj popcnt.obj
    gcc -o main.exe main.obj popcnt.obj

main.obj: main.c
    gcc -c -o main.obj main.c

popcnt.obj: popcnt.asm
    nasm -f win64 popcnt.asm

该内容编译并运行,但预期结果 (32) 似乎没有发生。

c windows function assembly nasm
1个回答
0
投票

解决方案其实很简单;只有两个说明:

popcnt rax, rcx
ret
© www.soinside.com 2019 - 2024. All rights reserved.