Using the assembly code below, please comment to the right of each code what eac
ID: 3714210 • Letter: U
Question
Using the assembly code below, please comment to the right of each code what each line does and at the end display the user input following by the reversed string.
INCLUDE Irvine32.inc
.data
stringInput byte 50 dup(0)
promptIn byte "Enter the string: ",0
.code
swaping MACRO a,b
XOR a,b
XOR b,a
XOR a,b
ENDM
strlen proc
push ebp
mov ebp, esp
push ebx
mov eax, 0
mov ebx, [ebp+8]
Counter:
cmp byte ptr [ebx + eax], 0
je Done
inc eax
jmp counter
Done:
pop ebx
mov esp, ebp
pop ebp
ret TYPE ebp
strlen endp
strrev proc
Push ebp
MOV ebp, esp
Push eax
Push ebx
Push ecx
Mov ebx, [ebp+8]
Mov ecx, [ebp+8]
push ebx
call strlen
ADD ecx, eax
SHR al,1
count: CMP al,0
JZ finished
MOV ah, byte PTR [ebx]
swaping ah, byte ptr [ecx-1]
MOV BYTE PTR [ebx], ah
INC ebx
DEC ecx
DEC al
JMP count
finished: POP ecx
POP ebx
POP eax
MOV esp, ebp
POP ebp
RET (TYPE ebp)*4
strrev ENDP
main proc
mov eax, 0
mov edx, offset promptIn
call writeString
mov edx, offset stringInput
mov ecx, 51
call readString
push edx
call strrev
call crlf
call writestring
call crlf
exit
main endp
end main
Explanation / Answer
INCLUDE Irvine32.inc // INCLUDE includes a file within the file being assembled
.data
stringInput byte 50 dup(0) // It will create a memory space
promptIn byte "Enter the string: ",0 // Here a prompt comes to Enter the string
.code
swaping MACRO a,b // It is a macro function which swaps the values of a and b
XOR a,b // It XORs the values of a and b
XOR b,a // It XORs the values of b and a
XOR a,b // It XORs the values of a and b
ENDM // Ends the body of macro
strlen proc // strlen is used to calculate the length of the string
push ebp //to hold the first address of the present function's workspace for local variable,used to access data on the stack it pushes the ebp onto the stack
mov ebp, esp // Pushing EBP onto the stack, and setting it to the value of the stack pointer (ESP)
establishes a starting point for the function’s own activation record(workspace for local variable) at the top of the stack, and a designated place to recover the caller's EBP value from. The latter is useful for when the function finishes, as the caller's activation record((workspace for local variable) must be restored before returning control to it.
push ebx //pushes this register on the stack,ebx is a general purpose register
mov eax, 0 //moves 0 into eax register
mov ebx, [ebp+8] //Move 8 bytes at memory address EBP + (8) into EBX
Counter:
cmp byte ptr [ebx + eax], 0 //compares the value pointed at memory location sum of ebx and eax with zero
je Done //if yes then jump to Done
inc eax // increase the value of memory location by 1 which is pointing to eax memory location
jmp counter // jump to counter
Done:
pop ebx // it will pop the ebx out from the stack
mov esp, ebp // it will move the value of register ebp to register esp
pop ebp //it will pop the ebp out from the stack
ret TYPE ebp //it will return to the calling function
strlen endp //it will calculate the length of the string
strrev proc // it will reverse the string
Push ebp // it will push ebp onto the stack
MOV ebp, esp // it will move the contents from register esp to ebp
Push eax // it will push the register eax into stack
Push ebx // it will push the register ebx into stack
Push ecx // it will push the register ecx into stack
Mov ebx, [ebp+8] //Move 8 bytes at memory address EBP + (8) into EBX
Mov ecx, [ebp+8] //Move 8 bytes at memory address EBP + (8) into EBX
push ebx //pushes the ebc onto the stack
call strlen // it will call the strlen function
ADD ecx, eax // add the contents of ecx and eax and store the result in ecx
SHR al,1 //SHR shifts the bits within the destination operand to the right, here 1 is written so it will shift one bit right
count: CMP al,0 // it will compare the value of al with zero
JZ finished // jump to finished if zero flag is set
MOV ah, byte PTR [ebx] // move the contents from memory location pointed by ptr to ah
swaping ah, byte ptr [ecx-1] // swap the contents of ah with memory location pointed by ptr
MOV BYTE PTR [ebx], ah // move the contents of ah to the memory location pointed by ebx
INC ebx // increase the value of ebx register by one
DEC ecx //decrease the value of ecx register by one
DEC al //decreases the value of al register by one
JMP count //jumps to count
finished: POP ecx //it will pop the ecx out from the stack
POP ebx //it will pop the ebx out from the stack
POP eax // it will pop the eax out from the stack
MOV esp, ebp // it will move the value of register ebp to register esp
POP ebp // it will pop the ebp out from the stack
RET (TYPE ebp)*4 //returns to the type ebp after multiplying its memory location by 4
strrev ENDP //it will reverse the string
main proc //to define the sequence of procedures
mov eax, 0 //it will move 0 to the eax
mov edx, offset promptIn // it will prompt the user a message and move the entered value to the edx
call writeString // calls the function writeString
mov edx, offset stringInput // moves the offset value at stringInput to edx
mov ecx, 51 //moves 51 to register ecx
call readString // calls the readString function
push edx //pushes the edx onto the stack
call strrev //calls the strrev function
call crlf //calls the crlf function
call writestring //calls the writestring function
call crlf //calls the crlf function
exit // program exits/terminates
main endp //denotes end of the procedure
end main //end of main
Enter the string:hello
olleh
If you have any query comment below and if you like my solution please give a thumbs up!
Thanks :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.