Home > Mobile >  When to use .byte over .ascii
When to use .byte over .ascii

Time:09-06

Let's say I want to declare a variable like "u", which is one character. Why would I want to use .byte instead of .ascii?

Wouldn't a single ascii character also be 1 byte long?

CodePudding user response:

For a single ASCII character, you might well choose either .ascii "u" or .byte 'u'. Both will assemble to one byte at the current position. That's the one use-case where .ascii and .byte's functionality pretty much overlaps.

For human readers, some might read the .ascii as char foo[1] = {'u'}; vs. the .byte being like char bar = 'u';, but in asm that's just a semantic difference. Both are 1-byte objects with the same object-representation, and unlike C there's nothing that will implicitly dereference one to get the value there vs. the name being the address for the array.


.byte allows non-ASCII values conveniently, like .byte 1, 2, 3, to implement C
uint8_t arr[] = {1,2,3};

You could equivalently write .ascii "\x01\x02\x03" if your assembler supports hex escape codes (GAS or clang), but for human readers the .byte version looks a lot more like C.

.byte also allows signed negative values like -2 without having to manually work out the 2's complement bit-pattern like .ascii "\xFE".

For text strings longer than 1 byte, .ascii "abc" is of course more convenient than
.byte 'a', 'b', 'c'.

  • Related