0

Having this nasm:

global _start
    section .text
_start:
    mov rax, 1 ;syscall number
    mov rdi, 1 ;fd [data index]
    mov rsi, msg3 ;buffer [source index]
    mov rdx, len2+10 ;sizeof(*buffer) [data]
    syscall
    mov rax, 60 ;syscall
    mov rdi, rdi ;exit code=0
    syscall

    section .data
msg db 'hello world', 0
msg2 db 'Goodbye!',0
len1 equ $ - msg
len2 equ $ -msg2
msg3 db 'len1=',len1,10,'len2=',len2,10

I want to embed symbols len1 len2 to the final buffer that goes to the rsi before write. I want to try the misuse the $ in that, the second len2 should have both strings size, as stated here: How does $ work in NASM, exactly? according to @Peter Cordes. How can I embed multiple objects (whether strings addresses or ascii chars or number or ...) as I can in c ? (printf("format whatever %s %i %c", string, int, char)). How to do that?

autistic456
  • 183
  • 1
  • 10
  • That puts the integer values into the array of bytes. If the lengths are small enough, you can do `len1 + '0'` to get an ASCII digit. Otherwise you'd need to try to use the NASM macro system so you can have it stringify the number into ASCII digits. Not sure if that's possible for `equ` constants; probably easier to define the strings via a macro so you can use macro stuff on the strings. – Peter Cordes Jun 08 '20 at 18:47
  • What does mean len1 + '0'? and what it depends on the size of the len? (as you said - *if the lengths are small enought*) – autistic456 Jun 09 '20 at 01:06
  • Single-digit integers only take 1 byte, of course, but larger numbers need a variable-length string. You need an assemble-time version of [How do I print an integer in Assembly Level Programming without printf from the c library?](https://stackoverflow.com/a/46301894). You could do `len1/10 + '0', len1%10 + '0'` to make it fixed-width zero padded for integers from 00..99. – Peter Cordes Jun 09 '20 at 04:09

0 Answers0