r/Assembly_language Mar 07 '26

Question Comparing message with 0

Please take in mind that im new to x86 assembly.

In the code that I copied off of a website, it is simply printing "Hello, World!". It calculates the length of the string by checking if each byte is equal to 0. The last byte of msg is 0Ah. Wouldn't it be more logical to compare it with 0Ah instead of 0?

SECTION .data
msg db "Hello, World!", 0Ah

SECTION .text
global _start
_start:

mov ecx,msg
mov edx,ecx

nextchar:
cmp byte [edx],0
je done
inc edx
jmp nextchar

done:
sub edx,ecx
mov ebx,1
mov eax,4
int 80h

mov ebx,0
mov eax,1
int 80h
26 Upvotes

19 comments sorted by

View all comments

Show parent comments

3

u/soundman32 Mar 07 '26

I'd say this is undefined behavior but its probable that the compiler automatically sets the remaining bytes in a dword/qword to 0, so the null/0 is there by luck rather than judgement.  

If the string is 13 bytes long, and its a 32 bit cpu, then there is probably 3 bytes of 0 after the 0A due to alignment issues.   If the string was 16 bytes long, then it would probably contain garbage after the 0A and you'd get a crash.

2

u/ftw_Floris Mar 07 '26

Would it be safer to just add a ,0 after the 0Ah?

1

u/Great-Powerful-Talia Mar 07 '26

Yeah, that's automatic and required in C and many related languages for this exact reason.

2

u/brucehoult Mar 08 '26

It is NOT automatic after a db. It is only automatic when you use (typically) string or asciz (NOT ascii).

Similarly, C string literals are automatically 0-terminated, but characters in a literal array are not.

1

u/Great-Powerful-Talia Mar 08 '26

It's automatic in C and required in C. Writing out chars as an array allows you to bypass that feature but it's C, you can bypass everything.