简体   繁体   English

如何编译64位汇编代码

[英]How to compile 64bit assembly code

I am using window 7 and don't know how to compile this assembly code.Do i need to calculate manually?.I want to know the better way to get faster or wish to know how to calculate line by line. 我正在使用窗口7,不知道如何编译此汇编代码。我需要手动计算吗?我想知道更快的更好方法,或者想知道如何逐行计算。 I need the final value of rax. 我需要rax的最终值。

start:
mov $1020, %rax
mov $4090, %rbx
mov $2044, %rcx
xor %rdx, %rdx
sub %rcx, %rbx
cmp %rbx, %rax
jge loopa
jmp loopb
loopa:
cmp $4, %rdx
jg end
inc %rdx
loopb:
xchg %rax, %rbx
idiv %rbx
add %rdx, %rax
imul %rcx
jmp loopa
end:

Given that this is in AT&T syntax, you probably want as and ld from the MinGW toolchain, that you can use to build an executable out of this. 鉴于这是AT&T语法,您可能希望MinGW工具链中的asld可以用来构建可执行文件。

You need to add a bit of extra boilerplate at the beginning to export the symbol of the entrypoint and tell the linker that the function goes into the .text segment: 您需要在开始时添加一些额外的样板来导出入口点的符号,并告诉链接器该函数进入.text段:

.text
.global start

Then you can assemble the file and link it: 然后,您可以汇编文件并将其链接:

as filename.as -o filename.o
ld filename.o -e start -o output.exe 

The -e option tells the linker what function will be the entrypoint of the executable. -e选项告诉链接器什么函数将成为可执行文件的入口点。

(I tested this on Linux, but the syntax should be the same in the MinGW toolchain on Windows) (我在Linux上进行了测试,但是语法在Windows上的MinGW工具链中应该相同)

Now, your assembly function doesn't perform any IO, and doesn't even terminate correctly, so if you run your executable you'll probably just get a segfault. 现在,您的汇编函数不会执行任何IO,甚至无法正确终止,因此,如果您运行可执行文件,则可能只会遇到段错误。 You have to run it into a debugger, adding a breakpoint at the end of the function and inspecting rax when the execution gets there. 您必须将其运行到调试器中,在函数末尾添加一个断点,并在执行到那里时检查rax

Another option is to embed this code into an asm block into a C file and perform the IO in C. Still, if you don't have any toolchain installed you may be able to churn out the result faster by hand. 另一个选择是将此代码嵌入到asm块中的C文件中,并在C中执行IO。但是,如果您未安装任何工具链,则可以手动更快地得出结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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