You are on page 1of 4

Ministry of Education, Culture and Research of the Republic of Moldova

Technical University of Moldova

Department of Software Engineering and Automatics

Laboratory work nr.1

Theme: NASM – printing methods

Elaborated by: st.gr.FAF-172 V.Boaghi

Verified by: dr. conf. univ. C.Rostislav

Chişinău – 2019
Method 1
Source code
section .text align=32
global _start
_start:
mov rax, 1
mov rdi, 1
mov rdx, lungimelinie
mov rsi, linie
syscall
mov rax, 1
mov rdi, 1
mov rdx, len
mov rsi, msg
syscall
mov rax, 60
mov rdi, 0
syscall
section .data
align 8
msg db 0xa, ' Boaghi Vasile(FAF-172) 22.02.2019 ', 0xa, 0xa, 0xa,
0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa
len equ $ - msg
linie times 10 db 0xA, 0xD
lungimelinie equ $-linie
Output

The main information that we need is to know where we should put the length of the
message(rds) and the message itself(rsi). In this case, the outputed string is kept in variable msg
and the length in len. To have a lot of free space between last and future commands we can use
simbol 0xa, or use the new line multiple times, by using times option. Also I’ve met some
problems using another method for calling kernel, int 0x80 was having an error: Segmentation
error (Core dumped). After analyzing this error, I found an alternative, syscall – so it worked
perfectly.
Method 2
Source code
extern printf
section .data
msg: db 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, " Boaghi Vasile(FAF-172)
22.02.2019 ", 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa
fmt: db "%s", 10, 0
section .text
global main
main:
push rbp
mov rdi,fmt
mov rsi,msg
mov rax,0
call printf
pop rbp
mov rax,0
ret

Output

Unlike the previoues method, this one is was easier, also it’s a perfect example to show that .data
section can be at the top of the program or at the bottom, also it doesn’t need string’s length,
bascially, it is cheaper than previoues one. You have just to declare C function, than call it.

Method 3
Source code
section .data
string1 db 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, " Boaghi Vasile(FAF-172)
22.02.2019 ", 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa

section .text
global _start
_start:
mov rdi, string1
xor rcx, rcx
not rcx
xor al,al
cld
repnz scasb
not rcx
dec rcx
mov rdx, rcx
mov rsi, string1
mov rax, 1
mov rdi,rax
syscall
xor rdi,rdi
mov rax, 0x3c
syscall
Output

This method is relatively close to the first one, it also needs the message’s length, but it is not
stored. Besides that, it uses index to iterate through the string1. It uses the same register as
previous two, also it constantly calls the kernel.

You might also like