2. (5 points) Use a loop instruction with indirect addressing to reverse the ele
ID: 3599208 • Letter: 2
Question
2. (5 points) Use a loop instruction with indirect addressing to reverse the elements of an array in place. - Do not copy the elements to any other array. Use the SIZEOF, TYPE, and LENGTHOF operators to make the program as flexible as possible if the array size and type should be changed in the future. - Use the XCHG instruction - The array's elements are: B, A, D, C, F, E, H, G. The array's elements after running the program should look like: A, B, C, D, E, F, G, H o Submit the following: Lastname2.asmExplanation / Answer
Explanation :
_________________________________________________________________
START is the label used to show the starting point of the code which is written in the Code Segment. : is used to define a label as in C programming.
Next Line – MOV AX,DATA
MOV DS,AX
After Assuming DATA and CODE Segment, Still it is compulsory to initialize Data Segment to DS register. MOV is a keyword to move the second element into the first element. But we cannot move DATA Directly to DS due to MOV commands restriction, Hence we move DATA to AX and then from AX to DS. AX is the first and most important register in the ALU unit. This part is also called INITIALIZATION OF DATA SEGMENT and It is important so that the Data elements or variables in the DATA Segment are made accessable. Other Segments are not needed to be initialized, Only assuming is enhalf.
Next Line – DISPLAY MSG1
DISPLAY MSG1 is Calling of a Macro DISPLAY with Argument MSG1. This will display String Msg1 on Screen.
Next Line – LEA DX,P1
MOV AH,0AH
INT 21H
The above three line code is used to Scan the String entered by user Onscreen to the variable length character Array present in P1 Label Byte and String refered by P11 as Array.
Now, lets understand line by line
LEA DX,P1 in this LEA stands for LOAD EFFECTIVE ADDRESS and it loads the effective address of second element into the first element. This same code can be interchangably written as MOV DX, OFFSET P1 where OFFSET means effective address and MOV means move second element into the first element.
MOV AH,0AH
INT 21H
The above two line code is used to SCAN the String entered by user Onscreen to the variable length character Array to the address present in DX.
Standard Input and Standard Output related Interupts are found in INT 21H which is also called as DOS interrupt. It works with the value of AH register, If the Value is 0AH, That means SCAN the String entered by user Onscreen to the variable length character Array to the address present in DX.
Next Line – DISPLAY MSG2
DISPLAY MSG1 is Calling of a Macro DISPLAY with Argument MSG2. This will display String Msg2 on Screen.
Next Line – MOV DL,L1
ADD DL,30H
MOV AH,2
INT 21H
The above Four line code is used to Write a Character on Console present in L1 Variable (i.e.Length of first String).
Standard Input and Standard Output related Interupts are found in INT 21H which is also called as DOS interrupt. It works with the value of AH register, If the Value is 2 or 2h, That means WRITE a Character on Console present in DL register hence the value to be printed is moved to DL register. Here we are printing L1 variable.
Next Line – DISPLAY MSG3
DISPLAY MSG3 is Calling of a Macro DISPLAY with Argument MSG3. This will display String Msg3 on Screen.
Next Line – LEA SI,P11
LEA DI,P22
The above Two line code is used to initialize P11 to SI registe rand P22 to DI register.
Next Line – MOV DL,L1
DEC DL
MOV DH,0
ADD SI,DX
The above Four line code is used to Jump SI to the Last Character of the String to be Reversed.
MOV DL,L1 is used to move L1 (i.e. Actual Length of first String Entered) to DL register DEC DL will decrement the value present in DL register by One and MOV DH,0 is used to move or assign value Zero (decimal value) to DH Register(We have to do all this because we can’t add DL with SI as DL is 8 bit and SI is 16 bit). After all this we can now add DX to SI by ADD SI,DX.
Next Line – MOV CL,L1
MOV CH,0
The above Two line code is used to Move L1 (i.e. Actual Length of String Entered) to CL register and MOV CH,0 is used to move or assign value Zero (decimal value) to CH Register.
Next Line – REVERSE:
REVERSE: is a LABEL and all the words ending in colon (:) are Labels.
Next Line – MOV AL,[SI]
MOV [DI],AL
Move value at Address of SI Register to AL register Move value of AL register to Address of DI Register as we want to copy Character from [SI] to [DI].
Next Line – INC DI
DEC SI
INC DI will increment the value present in DI register by One. DEC SI will decrement the value present in SI register by One.
Next Line – LOOP REVERSE
This end of loop. In assembly programming language we have a LOOP instruction. This works with two other helpers which are Label and Counter. The Loop start with LABEL and ends with LOOP instruction with the same LABEL name with it. the execution of the Loop depends on the value in CX register ( CX is also Called COUNTER).
Next Line – DISPLAY P22
DISPLAY P22 is Calling of a Macro DISPLAY with Argument P22, get the Converted String in p22 Variable and Display the Converted String Onscreen.
Next Line – MOV AH,4CH
INT 21H
The above two line code is used to exit to dos or exit to operating system. Standard Input and Standard Output related Interupts are found in INT 21H which is also called as DOS interrupt. It works with the value of AH register, If the Value is 4ch, That means Return to Operating System or DOS which is the End of the program.
Next Line – CODE ENDS
CODE ENDS is the End point of the Code Segment in a Program. We can write just ENDS But to differentiate the end of which is written in Code segment
THANK YOU :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.