Johanne Fairchild
2024-06-29 12:34:51 UTC
Taking my first steps with
The Art of 64-bit Assembly Language, Volume 1
Randall Hyde, No Starch Press, 2022
ISBN 978-1-7185-0109-6
I am being a bit cocky in trying to use the tools: instead of learning
to use cl, I'm trying to use gcc, which I'm more used to.
%ml64 /c sort.asm
Microsoft (R) Macro Assembler (x64) Version 14.29.30138.0
Copyright (C) Microsoft Corporation. All rights reserved.
Assembling: sort.asm
So far so good. Full source code of sort.asm at the end of this post.
%cat main.c
#include <stdio.h>
void asmproc(void);
int main(void) {
printf("Invoking asmproc...\n");
asmproc();
printf("Done.\n");
}
%gcc -c main.c
%gcc -o sortme.exe sort.obj main.o
sort.obj:(.text$mn+0x75): relocation truncated to fit: IMAGE_REL_AMD64_ADDR32 against `sortme'
collect2.exe: error: ld returned 1 exit status
The name /sortme/ is the name of an array in sort.asm.
%file sort.obj
sort.obj: Intel amd64 COFF object file, not stripped, 4 sections, symbol offset=0x25a, 16 symbols
%file main.o
main.o: Intel amd64 COFF object file, no line number info, not stripped, 7 sections, symbol offset=0x2b4, 23 symbols
(*) Sort.asm source code
option casemap:none
nl = 10
maxlen = 256
true = 1
false = 0
bool typedef ptr byte
.const
fmt byte "sortme[%d] = %d", nl, 0
.data
sortme label dword
dword 1, 2, 16, 14
dword 3, 0, 4, 10
dword 5, 7, 15, 12
dword 8, 6, 11, 13
ssize = ($ - sortme) / sizeof dword
didswap bool ?
.code
externdef printf:proc
sort proc
push rax
push rbx
push rcx
push rdx
push r8
dec rdx
outer: mov didswap, false
xor rbx, rbx
inner: cmp rbx, rdx
jnb xinner
mov eax, [rcx + rbx*4]
cmp eax, [rcx + rbx*4 + 4]
jna dntswp
mov r8d, [rcx + rbx*4 + 4]
mov [rcx + rbx*4 + 4], eax
mov [rcx + rbx*4], r8d
mov didswap, true
dntswp: inc rbx
jmp inner
xinner: cmp didswap, true
je outer
pop r8
pop rdx
pop rcx
pop rbx
pop rax
ret
sort endp
public asmproc
asmproc proc
push rbx
sub rsp, 40
lea rcx, sortme
mov rdx, ssize
call sort
xor rbx, rbx
disp: mov r8d, sortme[rbx*4]
mov rdx, rbx
lea rcx, fmt
call printf
inc rbx
cmp rbx, ssize
jb disp
add rsp, 40
pop rbx
ret
asmproc endp
end
The Art of 64-bit Assembly Language, Volume 1
Randall Hyde, No Starch Press, 2022
ISBN 978-1-7185-0109-6
I am being a bit cocky in trying to use the tools: instead of learning
to use cl, I'm trying to use gcc, which I'm more used to.
%ml64 /c sort.asm
Microsoft (R) Macro Assembler (x64) Version 14.29.30138.0
Copyright (C) Microsoft Corporation. All rights reserved.
Assembling: sort.asm
So far so good. Full source code of sort.asm at the end of this post.
%cat main.c
#include <stdio.h>
void asmproc(void);
int main(void) {
printf("Invoking asmproc...\n");
asmproc();
printf("Done.\n");
}
%gcc -c main.c
%gcc -o sortme.exe sort.obj main.o
sort.obj:(.text$mn+0x75): relocation truncated to fit: IMAGE_REL_AMD64_ADDR32 against `sortme'
collect2.exe: error: ld returned 1 exit status
The name /sortme/ is the name of an array in sort.asm.
%file sort.obj
sort.obj: Intel amd64 COFF object file, not stripped, 4 sections, symbol offset=0x25a, 16 symbols
%file main.o
main.o: Intel amd64 COFF object file, no line number info, not stripped, 7 sections, symbol offset=0x2b4, 23 symbols
(*) Sort.asm source code
option casemap:none
nl = 10
maxlen = 256
true = 1
false = 0
bool typedef ptr byte
.const
fmt byte "sortme[%d] = %d", nl, 0
.data
sortme label dword
dword 1, 2, 16, 14
dword 3, 0, 4, 10
dword 5, 7, 15, 12
dword 8, 6, 11, 13
ssize = ($ - sortme) / sizeof dword
didswap bool ?
.code
externdef printf:proc
sort proc
push rax
push rbx
push rcx
push rdx
push r8
dec rdx
outer: mov didswap, false
xor rbx, rbx
inner: cmp rbx, rdx
jnb xinner
mov eax, [rcx + rbx*4]
cmp eax, [rcx + rbx*4 + 4]
jna dntswp
mov r8d, [rcx + rbx*4 + 4]
mov [rcx + rbx*4 + 4], eax
mov [rcx + rbx*4], r8d
mov didswap, true
dntswp: inc rbx
jmp inner
xinner: cmp didswap, true
je outer
pop r8
pop rdx
pop rcx
pop rbx
pop rax
ret
sort endp
public asmproc
asmproc proc
push rbx
sub rsp, 40
lea rcx, sortme
mov rdx, ssize
call sort
xor rbx, rbx
disp: mov r8d, sortme[rbx*4]
mov rdx, rbx
lea rcx, fmt
call printf
inc rbx
cmp rbx, ssize
jb disp
add rsp, 40
pop rbx
ret
asmproc endp
end