OneCompiler

ASSIGNMENT 3

196

global _start

section .data
imsg db 10,'Enter the String : '
len1 equ imsgomsgdb10,LengthofGivenString:len2equ-imsg omsg db 10,'Length of Given String : ' len2 equ -omsg
nl db 10
len_nl equ nlsection.bssdispbuffresb2bufresb50buflenequ-nl section .bss dispbuff resb 2 buf resb 50 buf_len equ -buf
len resb 1

%macro print 2
mov rax, 1
mov rdi, 1
mov rsi, %1
mov rdx, %2
syscall
%endmacro
%macro accept 2
mov rax, 0
mov rbx, 0
mov rcx, %1
mov rdx, %2
syscall
%endmacro

%macro exit 0
mov rax, 60
mov rdi, 0
syscall
%endmacro

section .text
_start:
print imsg, len1 ;display input prompt msg
accept buf, buf_len
dec al ;Decrementing coubt for avoiding string termination character
mov [len], al ;Save Length
print omsg, len2 ;display output msg
mov bl, [len] ;Restore length
call display ;call display procedure
print nl, len_nl
exit

display:
	mov rdi, dispbuff	;point rdi to buffer
	mov rcx, 2		;load number of digits to display
	disp1:
		rol bl, 4	;rotate number to left by nibble
		mov dl, bl	;move lower byte in dl
		and dl, 0fh	;mask upper digit of byte in dl
		add dl, 30h	;add 30h to calculate ASCII code	
		cmp dl, 39h	;compare with 39h
		jbe skip	;if less than 39h skip adding 07
		add dl, 07h	;else add 07
	skip:
		mov [rdi], dl	;store ASCII code in buff
		inc rdi		;point to next byte
		loop disp1	;decrement count of digits to display
		
	print dispbuff, 2	;display max number
	ret