Follow the Lab instructions and policies as usual for this Lab. The submission criteria for log text files and the submission of Q&A remain same as previous labs. In this lab we are going to perform various string operation exercises in 6 parts.
Copying the given sentence: Read and display the given sentence as part of this exercise. Use this sentence ("Low Level Assembly Programming") to read and display using assembly level language.
Code is already provided for copy_string.asm, as shown below. Copy the code as it is and follow the below instructions for execution.
section .data
string db "Low Level Assembly Programming",0
len equ $-string
section .bss
copy resb len
section .text
global main
extern puts
main:
mov rcx,len ; Use 64-bit registers
xor rdx,rdx ; Clear rdx register
lp:
xor rax,rax ; Clear rax register
mov al,byte[string+rdx] ; Access byte from string
mov byte[copy+rdx],al ; Copy byte to copy
inc rdx ; Increment rdx
cmp rdx,rcx ; Compare rdx with rcx (length of string)
jl lp ; Jump to lp if rdx is less than rcx
endof:
lea rdi, [copy] ; Load address of copy into rdi (64-bit pointer)
call puts ; Call puts function
ret ; Return
[XXXXXXX@gsuad.gsu.edu@snowball ~]$ nasm -f elf64 copy_string.asm -o copy_string.o
[XXXXXXX@gsuad.gsu.edu@snowball ~]$ gcc -m64 -o copy_string copy_string.o
[XXXXXXX@gsuad.gsu.edu@snowball ~]$ ./copy_string
Expected output of this exercise:
Answer the following Questions for Part1
Question 1: Explain the purpose of the lea instruction used in the endof section?
Question 2: What does the jl instruction do in the loop (lp)?
Question 3: What would happen if the copy buffer was not allocated enough space to hold the entire string?
Question 4: How does the code ensure that the copied string (copy) is null terminated?
Question 5: Explain the purpose of the puts function and how it interacts with the main function?
Below code segment will be helpful to achieve this exercise:
main:
xor rax, rax ; Clear rax
xor rcx, rcx ; Clear rcx (counter for string length)
mov rdi, string ; Load the address of the string into rdi
lp:
cmp byte [rdi + rcx], 0 ; Compare the current character to null terminator
je endof ; If it's null, jump to endof
inc rcx ; Increment the counter
jmp lp ; Repeat the loop
Expected output of this exercise: 30
Answer the following Questions for Part2
Question 1: How does the xor rax, rax instruction clear the rax register, and why is it necessary to clear it before use?
Question 2: Explain the purpose of using xor rcx, rcx to clear the rcx register before counting the length of the string?
Question 3: What is the significance of the instruction cmp byte [rdi + rcx], 0 in the loop, and how does it contribute to the termination condition?
Question 4: If the string were to contain non-ASCII characters or multi-byte characters, how might that affect the accuracy of this code in calculating the length of the string?
Question 5: Suggest an alternative approach to counting the length of the string without using a loop. How might this alternative approach differ in terms of efficiency or complexity compared to the given code?
Below code segment will be helpful to achieve this exercise:
main:
mov rsi, string ; Load the address of string into rsi (64-bit register)
xor rcx, rcx ; Clear rcx register
lp:
cmp byte [rsi], 0 ; Compare byte at address rsi with 0
jz endof ; If null terminator is reached, jump to endof
cmp byte [rsi], 'i' ; Compare byte at address rsi with 'i'
jne nextchar ; If not equal, jump to nextchar
inc rcx ; Increment rcx if 'i' found
Expected output of this exercise: 3
Answer the following Questions for Part3
Question 1: Explain the difference between cmp and test instructions in assembly language?
Question 2: What is the purpose of the jz instruction in assembly language, and how does it relate to the Zero Flag (ZF)?
Question 3: How does the inc instruction differ from the add instruction in assembly language?
Question 4: Describe the purpose of the jne instruction in assembly language and provide an example scenario where it might be used?
Question 5: Explain the concept of addressing modes in assembly language and provide examples of different addressing modes?
As a part of this exercise, count a space in the given sentence. For this exercise use this sentence "Spaces are going to count from here so we are giving more and more and more". In this sentence count how many times ' ' appears.
Below code segment will be helpful to achieve this exercise:
lp:
cmp byte [rsi], 0 ; Compare byte at address rsi with 0
jz endof ; If null terminator is reached, jump to endof
mov al, ' ' ; Load ' ' into al register
cmp byte [rsi], al ; Compare byte at address rsi with ' '
jnz lp2 ; If not equal, jump to lp2
inc rcx ; Increment rcx if ' ' found
Expected output of this exercise: 15
Answer the following Questions for Part 4
Question 1: Explain the significance of the cmp byte [rsi], 0 instruction in the loop. How does it contribute to the termination condition?
Question 2: What is the purpose of loading the value ' ' into the al register before comparing it with the byte at address rsi?
Question 3: Describe the role of the jnz lp2 instruction in the code segment. When and why would the program jump to the lp2 label?
Question 4: How does the inc rcx instruction increment the value of rcx in this context? What role does rcx play in the code?
Question 5: Consider scenarios where the string contains multiple consecutive spaces. How might such scenarios affect the efficiency or accuracy of the space counting algorithm implemented in this code segment?
For this exercise use the below sentence and try to achieve the mentioned output.
"This string is going to be reversed."
Expected output of this exercise:
.desrever eb ot gniog si gnirts sihT
Use the below mentioned word to check whether given word is palindrome or not? "NITIN". In this approach perform reversing the given string and compare the given string and reversed string. If the string is same as reverse of it, then print a message as Palindrome else print a message as Not A Palindrome
Expected output of this exercise:
Palindrome
In this lab, we have learned: