Min Ko Ko
Min Ko Ko

Reputation: 141

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. I need the final value of 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:

Upvotes: 0

Views: 2478

Answers (1)

Matteo Italia
Matteo Italia

Reputation: 126787

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.

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
.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.

(I tested this on Linux, but the syntax should be the same in the MinGW toolchain on Windows)

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. 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.

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.

Upvotes: 2

Related Questions