1

I try to print b, but it prints nothing. Also I wanted to find length of b to hardcode it (because dws have the same length), but it printed nothing too (that's why I created lenlen — len of blen, so that I can print blen). Don't pay attention to k and a, they are not used yet.

    global _main:
    section .data
b:  dw 10
blen: equ $-b
lenlen: equ $-blen
k:  dw 6
    section .bss
a:  resw 1
    section .text
_main:

    mov rax, 0x2000004
    mov rdi, 1
    mov rsi, b
    mov rdx, blen
    syscall

    mov rax, 0x2000001
    mov rdi, 0
    syscall
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Martian
  • 227
  • 1
  • 15
  • 1
    sys_write system call doesn't print numbers. It only prints ascii strings (and there isn't a system call that prints number). You will have to convert the number to a string and pass that string to sys_write. – Michael Petch Jun 07 '20 at 15:49
  • @MichaelPetch to do it I just have to move contents of `b` to a register? – Martian Jun 07 '20 at 15:52
  • No, you need to convert it to a string. You need to build the string with the number in it and pass the address of that new string to sys_write. One way to convert a number to a string is repeated division by 10. – Michael Petch Jun 07 '20 at 15:53
  • 1
    [How do I print an integer in Assembly Level Programming without printf from the c library?](https://stackoverflow.com/a/46301894) has 64-bit code (and a Linux `write` system call; easily adapted to OS X's call number) – Peter Cordes Jun 07 '20 at 16:05
  • @PeterCordes I'm actually ok with using C functions, but when I try to use `printf` (or `_printf` for macs?), it says that `Mach-O 64-bit format does not support 32-bit absolute addresses`. – Martian Jun 07 '20 at 16:11
  • There is an algorithm to convert an integer to an ascii string in this answer stackoverflow.com/a/56483319/3857942 . – Michael Petch Jun 07 '20 at 16:13
  • @MichaelPetch here they add/subtract '0' to convert to ascii: https://www.tutorialspoint.com/assembly_programming/assembly_arithmetic_instructions.htm – Martian Jun 07 '20 at 20:27
  • 1
    That works if dealing with a single digit number. It doesn't work if you have more than 1 digit. – Michael Petch Jun 07 '20 at 21:25
  • You can of course `call _printf` if you link libc: see [Why is %eax zeroed before a call to printf?](https://stackoverflow.com/q/6212665) for AT&T syntax examples of the calling convention. – Peter Cordes Jun 08 '20 at 03:51

0 Answers0