1

This will be very ammeter question. But I am new to assembly coding so bear with me here.

I am trying to make print function as a library that print numbers in assembly(at&t, x86_64) using syscall (write function / redirect to stdout). The lib file is linked to the main file where the function is called and the print value is stored inside the rax register before calling the function printNumber.

Code inside lib

.global num, printNumber

.section .data
num: .quad 0

.section .text

 printNumber:

mov %rax, %r9
mov $10, %r8

printTop:

xor %rax, %rax
xor %rdx, %rdx

mov %r9, %rax
div %r8

mov %rdx,  $num
add $0x30, $num
mov %rax, %r9


mov $1, %rax
mov $1, %rdi
mov $1, %rdx
mov $num, %rsi
syscall

cmp $0, %r9
jne printTop


ret

The problem is that it seems that I cannot write to the variable: num.

mov %rdx,  num
add $0x30, num

When I use it as lib do i get the error segmentation fault, But when i use it as a normal executable so does it work.

So my question is how do I rewrite the value inside and print it?

num: .quad 0

If there is no way to rewrite values in assembly how do point values to the stdout

mov $1, %rax   // Write syscall
mov $1, %rdi   // fd where to write/stdout
mov $1, %rdx   // Number of values to write
mov $num, %rsi // Pointer to where the output starting from
syscall

Have tried to use set the rbx register with the value instead but wont print anything, I guess that all registers is cleared for syscalls and cannot be used as holders.

mov $num, %rsi
to
mov %rbx, %rsi

//Note: I know that this function will only print unsigned integers reversed, But I am trying to solve the write problem for the moment.

Also I have tried to find solutions, but many are either in x86 or structured as intel.

Main Code


.data
    format: .asciz "%d\n"
    c: .quad 0

.text
.globl main
main:
    push    $1337
    pop     c
    push    c
    pop %rax
    call printNumber

movq $60, %rax
movq $0, %rdi
syscall

Build

Lib build.
gcc libCalc.s -no-pie -nostdlib -o libCalc.o
ar rcs ../lib/libCalc.a libCalc.o


Build and link.
gcc -no-pie -o bin/file file.s lib/libCalc.a

J4ck5ilver
  • 23
  • 4
  • `$num` means the address of `num`. You obviously can't change that. If you want to refer to the value, you must not use the `$`. What error do you get if you do that? You only say _"I got this working when I remove the $ sign, but not as a lib file just as a ordinary executable."_ It should work the same in a lib. – Jester Nov 22 '20 at 01:40
  • I get Segmentation fault, and if i remove mov %rdx, num and add $0x30, num i get nothing, Thx btw for the $ comment I have used it wrong. – J4ck5ilver Nov 22 '20 at 01:51
  • 1
    Well that's a different problem. [edit] your question with that code along with information from a debugger about where the fault is. Also show how you are calling it, and how you are assembling and linking. – Jester Nov 22 '20 at 01:52
  • Anyway, that sounds like you managed to put `num` into read-only section somehow. Double check this is the exact code you are running and you have `.section .data` as shown (or just `.data`). – Jester Nov 22 '20 at 01:57
  • @Jester: A *shared* library can't use 32-bit absolute addressing modes, and perhaps they tried to link a static library into a PIE executable. `mov %rdx, num` uses a 32-bit absolute addressing mode, unlike `mov %rdx, num(%rip)`. – Peter Cordes Nov 22 '20 at 02:00
  • Right, but that's not a segfault, it's a linker error (at least on my system). Also, I assume the "worked as an executable" means it's not PIE. – Jester Nov 22 '20 at 02:04
  • i use pop %rax and call printNumber from main, and yes this is the code I use did just check it row by row. Can i set num to different types of usage? – J4ck5ilver Nov 22 '20 at 02:04
  • Show your `main`. Randomly popping stuff from the stack is likely to cause a crash. Your code works here as a static library if I just do `mov $42, %eax; call PrintNumber; ret`. Assuming your `main` is really `_start` then popping rax could make sense, but then make sure you have an exit syscall not a `ret`. Even so, it should first print the number of arguments and only crash later :) – Jester Nov 22 '20 at 02:07
  • Have added the main Code – J4ck5ilver Nov 22 '20 at 02:12
  • Works here. Show how you are creating the library and linking. Make sure your library is not accidentally containing some wrong version. Also, use a debugger to pinpoint the fault. – Jester Nov 22 '20 at 02:12
  • Compiling as lib. gcc libCalc.s -no-pie -nostdlib -o libCalc.o ar rcs ../lib/libCalc.a libCalc.o Build and linking. gcc -no-pie -o bin/file file.s lib/libCalc. – J4ck5ilver Nov 22 '20 at 02:16
  • Ah, that's wrong. Funny that even works without linker error. You are putting an executable into the library. You want `gcc libCalc.s -c -o libCalc.o` – Jester Nov 22 '20 at 02:19
  • Thx It worked :D – J4ck5ilver Nov 22 '20 at 02:22

1 Answers1

4

Your problem is that gcc libCalc.s -no-pie -nostdlib -o libCalc.o is creating an executable instead of an object file even though it is named libCalc.o.

You want gcc libCalc.s -c -o libCalc.o to create a true object file you can then put into your library.

Jester
  • 56,577
  • 4
  • 81
  • 125