So I've been trying to figure out how I'd write a little function in assembly and then call it from C++, and so far I've been able to call the function and it does do
something, but not exactly what I wanted. I'm trying to basically write a function in assembly to output to the terminal,but when I call it, I get this weird extra characters in front of the message that I passed, so I'm not sure but I think im assigning something wrong or something I'm really not sure. I tried looking up the calling conventions for C stuff, and I found this link:
https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI which I assume is right since im on macos, but attempting to use
RSI and
RDI instead of the stack to get my arguments just made the write function do nothing at all, so I'm really not sure what im doing here...
my code looks like this:
try.s:
1 2 3 4 5 6 7 8 9
|
.globl _write
.text
_write:
movq $0x2000004, %rax
movq $1, %rdi
movq -16(%rbp), %rdx
movq -8(%rbp), %rsi
syscall
ret
| |
write.cpp:
1 2 3 4 5 6 7 8
|
extern "C" void write(const char *buf, long long len);
int main() {
// const char *msg = "hello world\n";
// const int len = 14;
// write(msg, len);
write("hello world\n", 14);
}
| |
and then i compile it with
clang++ try.s write.cpp
, and when running it I end up getting (very consistently)
so I don't know how but I'm somehow just not getting the pointer argument right I guess?? I'm really not sure.