rohit sharma
rohit sharma

Reputation: 191

Why is 0 included at the end of a string variable in assembly?

This is the normal way to declare a variable of type byte in assembly:

msg0 BYTE "string_1 in upper case: ",0

What's the need to manually specify ,0? It probably marks the end of the string.

But isn't the end of the string obvious once we close the double quotes?

Upvotes: 2

Views: 1633

Answers (1)

Peter Cordes
Peter Cordes

Reputation: 363950

There's no implicit terminating zero appended by close-quotes because you don't always want that. e.g. for passing to a write system call that takes a length, you just want the ASCII bytes and a length (explicit-length string), not an implicit-length 0-terminated C string.

e.g.

msg  db "hello"
msglen = $ - msg

Or as part of a struct or something, effectively defining a fixed-width char buf[4] or something where all the uses take all 4 bytes, not searching for a terminating 0.

Upvotes: 2

Related Questions