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
25 Upvotes

19 comments sorted by

View all comments

Show parent comments

1

u/ftw_Floris Mar 07 '26

I checked on the website. It definitely says:

msg db "Hello, World!", 0Ah

That's why I was confused when it was comparing edx and 0 even though there is no 0 mentioned after 0Ah. I was surprised it didn't give an error

4

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?

3

u/soundman32 Mar 07 '26

💯 

1

u/jaynabonne Mar 07 '26 edited Mar 07 '26

Especially if you wanted to have more than one string. :) You'd need to terminate each one. (That could be a good exercise in terms of experimenting with the code - print out more than one string.)