I have this piece:
.global main
.data
helloworld: .ascii "Hello world\n"
helloworldend:
goodluck: .asciz "Good Luck!\n"
goodluckend:
.text
main:
# printf(helloworld)
movq $1, %rax
movq $1, %rdi
movq $helloworld, %rsi
movq $helloworldend-helloworld, %rdx
syscall
# printf(goodluck)
movq $1, %rax
movq $1, %rdi
movq $goodluck, %rsi
movq $goodluckend-goodluck, %rdx
syscall
xorq %rax, %rax
ret
And I have to somehow make it also print "Hello Luck" without changing the data section. What I did is add a new data section inside the main:
...
main:
.section .data
msg: .ascii "Hello Luck\n"
msgend:
.section .text
start:
movq $1, %rax
movq $1, %rdi
movq $msg, %rsi
movq $msgend-msg, %rdx
syscall
...
And obviously that works but I'm not sure that that was the idea.. Is there a different way of printing it without adding a new data section and without changing the existing one?
CodePudding user response:
Kernel syscall sys_write
expects RDX
=number of bytes and RSI
=pointer to the string, which doesn't have to be null-terminated, so just change those two registers:
main:
# printf("Hello ")
movq $1, %rax
movq $1, %rdi
movq $helloworld, %rsi ; Pointer to "Hello World\n".
movq $6, %rdx ; Size of "Hello ".
syscall
# printf("Luck\n")
movq $1, %rax
movq $goodluck 5, %rsi ; Pointer to "Luck\n".
movq $5, %rdx ; Size of "Luck\n".
syscall
xorq %rax, %rax
ret