Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String in Assembly

What does .string do in assembly?

In an inline asm call in c, I wrote

.string \"Hello World\"

1) Can somebody give me an overview of how .string works?

2) Somehow that value gets saved in %esi. Why?

3) How can I append a return carriage on the end? \n doesn't work

like image 527
Gabe Avatar asked Oct 04 '11 10:10

Gabe


Video Answer


2 Answers

.string is an assembler directive in GAS similar to .long, .int, or .byte. All it does is create and initialize memory for you to use in your program. The .string directive will automatically null-terminate the string with [\0] for you. In this case, the assembler is allocating space for 14 characters in 14 contiguous bytes of memory. If you were to look at the memory, you would see this:

["][H][e][l][l][o][ ][W][o][r][l][d]["][\0]

Except in hexadecimal rather than characters. I'm not sure how or why %esi points to the string (it's either an assembler setting I'm not familiar with or has been set that way on a line of code you're not showing us). What it means is that %esi "points" to the first byte of the string - in this case the first ["]. You can use the %esi register to loop through the bytes using an offset (%esi + 5 would be [o]) or by incrementing the register itself.

To add a newline you might want to try \x0D\x0A instead of \n.

like image 84
Sparafusile Avatar answered Oct 27 '22 23:10

Sparafusile


  1. It just emits a sequence of characters into the appropriate code/data section. See this and this (they use .ascii, though).
  2. Show us the code.
  3. Try \\n or \12 or \xa.
like image 45
Alexey Frunze Avatar answered Oct 28 '22 00:10

Alexey Frunze