To display length of the String
Assignment02 Assembly Programming
Write an X86/64 ALP to accept a string and to display its length.
;To display length of the String
section .data
msg db "To display length of the String",0xA
len equ $-msg
msg1 db "Enter a string:" ,0xA
len1 equ $-msg
msg2 db "Length of string:" ,0xA
len2 equ $-msg2
cnt db 05h
section .bss
str : resb 16h
length resb 16h
section .text
global _start
_start :
; Display "To display length of the String"
mov rax,01
mov rdi,01
mov rsi,msg
mov rdx,len
syscall
; Display "Enter a string:"
mov rax,01
mov rdi,01
mov rsi,msg1
mov rdx,len1
syscall
; Read string
mov rax,00
mov rdi,00
mov rsi,str
mov rdx,16
syscall
; Display "To display length of the String"
mov rax,01
mov rdi,01
mov rsi,msg2
mov rdx,len2
syscall
; Convert hex to ascii and store in length buffer
; rax contains the length of the input string in hexadecimal
; cnt is used as a counter to keep track of the number of nibbles processed
dec rax ; decrement rax to start processing the last nibble first
mov r8, length ; r8 points to the beginning of the length buffer
mov byte[cnt],16 ; initialize the counter to 16 (the length of the buffer)
up:
rol rax,04 ; rotate rax left by 4 bits to get the next nibble
mov bl,al ; move the rotated value to bl
AND bl,0Fh ; keep only the last nibble of the value
cmp bl,09h ; compare bl with 09h (the ASCII value for '9')
jbe l1 ; if bl is less than or equal to '9', jump to label l1
add bl,7h ; add 7 to bl to convert the hex digit to the appropriate letter from A to F
l1:
add bl,30h ; add 30h to bl to convert the hex digit to its corresponding ASCII value
mov byte[r8], bl ; move the ASCII value to the current position in the length buffer
inc r8 ; move to the next position in the length buffer
dec byte [cnt] ; decrement the counter
jnz up ; if there are more nibbles to process, jump to label up
; Display length of string
mov rax,01
mov rdi,01
mov rsi,length
mov rdx,16
syscall
; Exit program
mov rax,60
mov rdi,00
syscall
Output:
Explanation
This code is an implementation of a program in x86 assembly language to find the length of a user-entered string and display it on the screen.
The program starts by printing a prompt message "To display length of the String" and "Enter a string:". The user is then prompted to enter a string of up to 16 characters, which is read into a buffer named str
.
The program then uses a loop to convert the hex value of the user input into an ASCII character, and stores it in a buffer named length
. The length of the string is calculated by counting the number of characters stored in the length
buffer.
The loop starts by decrementing the value of the register rax
, which holds the number of characters in the string. It then initializes a counter cnt
to 05h and a pointer r8
to point to the beginning of the length
buffer.
The loop then uses the rol
instruction to rotate the contents of the rax
register to the left by 4 bits, effectively shifting the current hex value of the string to the right by one digit. The value of the rotated hex digit is then extracted from the al
register and stored in the bl
register. The AND
instruction is used to mask the bl
register with 0Fh
, which zeroes out the top 4 bits and extracts the lower 4 bits, which represent the hex value of the character.
The program then checks whether the value in the bl
register is less than or equal to 9 (0-9 represent the first ten hexadecimal digits), and jumps to label l1
if it is. If bl
is greater than 9, it adds 7 to its value (to convert the hex digit to the corresponding ASCII character for values A-F). The program then adds 30h
to bl
to get the ASCII code for the digit. The resulting ASCII character is then stored in the buffer pointed to by r8
, and the pointer is incremented to point to the next byte in the buffer.
This process is repeated for each hex digit in the input string until all the hex digits have been processed and converted into ASCII characters. The program then uses the syscall
instruction to print the length of the string, which is stored in the length
buffer, to the console. Finally, the program exits using the syscall
instruction.
In summary, this program demonstrates how x86 assembly language can be used to convert hex values into ASCII characters, and provides an example of how to read and process user input in assembly language.