Copy Link
Add to Bookmark
Report

NULL mag Issue 06 22 Assembly tutorial 6

eZine's profile picture
Published in 
null magazine
 · 26 Dec 2020

  



NULL terminating bytes
Ok so why did our second message print twice when we only called our
sprint function on msg2 once? Well actually it did only print once. You
can see what I mean if you comment out our second call to sprint. The
output will be both of our message strings.
But how is this possible?
What is happening is we weren't properly terminating our strings. In
assembly, variables are stored one after another in memory so the last
byte of our msg1 variable is right next to the first byte of our msg2
variable. We know our string length calculation is looking for a zero
byte so unless our msg2 variable starts with a zero byte it keeps
counting as if it's the same string (and as far as assembly is concerned
it is the same string). So we need to put a zero byte or 0h after our
strings to let assembly know where to stop counting.
Note: In programming 0h denotes a null byte and a null byte after a
string tells assembly where it ends in memory.

helloworld-inc.asm

; Hello World Program (NULL terminating bytes)
; Compile with: nasm -f elf helloworld-inc.asm
; Link with (64 bit systems require elf_i386 option): ld -m elf_i386
; helloworld-inc.o -o helloworld-inc
; Run with: ./helloworld-inc

%include 'functions.asm'

SECTION .data
msg1 db 'Hello, brave new world!', 0Ah, 0h
msg2 db 'This is how we recycle in NASM.', 0Ah, 0h

SECTION .text
global _start

_start:

mov eax, msg1
call sprint

mov eax, msg2
call sprint

call quit
~$ nasm -f elf helloworld-inc.asm
~$ ld -m elf_i386 helloworld-inc.o -o helloworld-inc
~$ ./helloworld-inc
Hello, brave new world! This is how we recycle in NASM.



← previous
next →
loading
sending ...
New to Neperos ? Sign Up for free
download Neperos App from Google Play
install Neperos as PWA

Let's discover also

Recent Articles

Recent Comments

Neperos cookies
This website uses cookies to store your preferences and improve the service. Cookies authorization will allow me and / or my partners to process personal data such as browsing behaviour.

By pressing OK you agree to the Terms of Service and acknowledge the Privacy Policy

By pressing REJECT you will be able to continue to use Neperos (like read articles or write comments) but some important cookies will not be set. This may affect certain features and functions of the platform.
OK
REJECT