Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When to use .byte over .ascii

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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'.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading