4085 Cao Ass6
4085 Cao Ass6
b. .data Section:
Contains several strings used for user prompts and messages.
m1 prompts the user to enter a string.
m2 is for displaying the length of the string.
m3 will be used for displaying the reversed string.
c. .bss Section:
Reserves memory for various variables and buffers.
string is allocated 50 bytes to store the user's input string.
string2 is allocated 50 bytes to store the reversed string.
length is allocated 16 bytes for storing the length of the input string.
answer is allocated 16 bytes for displaying results.
d. .text Section:
The main code section.
global _start:
Declares the _start label as the entry point for the program.
e. Accept String:
Prompts the user to enter a string and stores it in the string buffer.
Calculates the length of the string and stores it in the length variable.
f. Display Procedure:
Converts the length of the string to its hexadecimal representation and stores it in the
answer buffer.
Reverses the hexadecimal representation in place.
g. Reverse String:
Reverses the original string by copying characters from string to string2 in reverse order.
%macro print 2
mov rax,1 ; Function 1 - write
mov rdi,1 ; To stdout
mov rsi,%1 ; String address
mov rdx,%2 ; String size
syscall ; invoke operating system to WRITE
%endmacro
%macro read 2
mov rax,0 ; Function 0 - Read
mov rdi,0 ; from stdin
mov rsi,%1 ; buffer address
mov rdx,%2 ; buffer size
syscall ; invoke operating system to READ
%endmacro
section .data
m1 db 10d,"Enter String: "
l1 equ $-m1
m2 db 10d,"Length of String: "
l2 equ $-m2
m3 db 10d,"Reversed String: "
l3 equ $-m3
section .bss
string resb 50
string2 resb 50
length resb 16
answer resb 16
section .text
global _start
_start:
print m1,l1
read string,50
;length is returned in rax
;decrement once to remove count of Enter character
;dec rax
mov [length],rax
print m2,l2
mov rax,[length]
mov rsi,answer+15
mov rcx,16
loop1: mov rdx,0
mov rbx,16
div rbx
cmp dl,09h
jbe skip1
add dl,07h
skip1: add dl,30h
mov [rsi],dl
dec rsi
dec rcx
jnz loop1
print answer,16
mov rsi,string
mov rdi,string2
mov rcx,[length]
add rdi,rcx
dec rdi
loop2:
mov al,[rsi]
mov [rdi],al
dec rdi
inc rsi
loop loop2
print m3,l3
print string2,[length]
mov rax,60
mov rdx,0
syscall