Question

In: Computer Science

Assembly Language. Write a procedure that reverses order of a 1-D array using the stack. The...

Assembly Language. Write a procedure that reverses order of a 1-D array using the stack. The array is a string array, and the address along with the number of elements are passed through the stack. Then write a complete program to test your procedure.

Solutions

Expert Solution

As per your question, First I will describe the steps involved in-order to reverse a 1-D string array. Next, I will write the procedure in 8086 assembly language(please refer to in-line comments for better understanding). Further, The complete 8086 assembly program for validating the procedure. So, please bear with me:-

The basic steps for reversal of any string array are followings:-

  1. Define or create a valid string
  2. Iterate or Traverse through each character of the string
  3. Push each character of the string in the stack
  4. Count the number of characters
  5. Load the beginning address of the string
  6. POP out every top character of the stack until the count is not equal to zero
  7. Put the character and reduce the count and increase the address accordingly
  8. Continue until the count is greater than zero
  9. Load the effective address of the string in dx using the LEA command
  10. Print the string by calling the interrupt with 9H in AH
  11. The string must be terminated by the ‘$’ sign

Now, the procedure of string reversal as per your question:-

REVERSE PROC 
    ; load the offset of 
    ; the string  
    MOV SI, OFFSET STRING  
  
    ; count of characters of the;  
    ;string  
    MOV CX, 0H  
  
    LOOP1: 
    ; compare if this is;  
    ;the last character  
    MOV AX, [SI]  
    CMP AL, '$'
    JE LABEL1  
  
    ; else push it in the;  
    ;stack  
    PUSH [SI]  
  
    ; increment the pointer;  
    ;and count  
    INC SI  
    INC CX  
  
    JMP LOOP1  
  
    LABEL1: 
    ; again load the starting;  
    ;address of the string  
    MOV SI, OFFSET STRING  
  
        LOOP2:  
        ;if count not equal to zero  
        CMP CX,0  
        JE EXIT  
  
        ; pop the top of stack  
        POP DX  
  
        ; make dh, 0  
        XOR DH, DH  
  
        ; put the character of the;  
        ;reversed string  
        MOV [SI], DX  
  
        ; increment si and;  
        ;decrement count  
        INC SI  
        DEC CX  
  
        JMP LOOP2  
  
                  
    EXIT: 
    ; add $ to the end of string  
    MOV [SI],'$ '
    RET  
          
REVERSE ENDP

=> The entire program in order to test the above procedure:-

.MODEL SMALL 
.STACK 100H 
.DATA 

; The string to be printed 
STRING DB 'God Bless You', '$'

.CODE 
MAIN PROC FAR 
MOV AX,@DATA 
MOV DS,AX 

; call reverse function 
CALL REVERSE 

; load address of the string 
LEA DX,STRING 

; output the string 
; loaded in dx 
MOV AH, 09H 
INT 21H 

; interrupt to exit
MOV AH, 4CH 
INT 21H 

MAIN ENDP 
REVERSE PROC 
        ; load the offset of 
        ; the string 
        MOV SI, OFFSET STRING 

        ; count of characters of the; 
        ;string 
        MOV CX, 0H 

        LOOP1: 
        ; compare if this is; 
        ;the last character 
        MOV AX, [SI] 
        CMP AL, '$'
        JE LABEL1 

        ; else push it in the; 
        ;stack 
        PUSH [SI] 

        ; increment the pointer; 
        ;and count 
        INC SI 
        INC CX 

        JMP LOOP1 

        LABEL1: 
        ; again load the starting; 
        ;address of the string 
        MOV SI, OFFSET STRING 

                LOOP2: 
                ;if count not equal to zero 
                CMP CX,0 
                JE EXIT 

                ; pop the top of stack 
                POP DX 

                ; make dh, 0 
                XOR DH, DH 

                ; put the character of the; 
                ;reversed string 
                MOV [SI], DX 

                ; increment si and; 
                ;decrement count 
                INC SI 
                DEC CX 

                JMP LOOP2 

                                
        EXIT: 
        ; add $ to the end of string 
        MOV [SI],'$ '
        RET 
                
REVERSE ENDP 
END MAIN 

=> The Run output:-

uoY sselB doG

Please don't forget to like it...

Thank You...


Related Solutions

Write a method public static Stack reverse(Stack s) that reverses the order of elements on stack...
Write a method public static Stack reverse(Stack s) that reverses the order of elements on stack s using a Queue. Test your method using some example stacks. In java
Using the windows 32 framework , write an assembly language program ; write a procedure to...
Using the windows 32 framework , write an assembly language program ; write a procedure to read a string and shift each character of the string by one. As an example, if your input string is Abcz, your output should be Bcda. Note that you must test your string for non-alphabetic characters (such as numbers and special characters). If there are non-alphabetic characters, you should terminate your program with an appropriate message. You should display your converted string using the...
Need to code this is assembly language. Using the windows32 framework, write a procedure to read...
Need to code this is assembly language. Using the windows32 framework, write a procedure to read a string and shift each character of the string by one. As an example, if your input string is Abcz, your output should be Bcda. Note that you must test your string for non-alphabetic characters (such as numbers and special characters). If there are non-alphabetic characters, you should terminate your program with an appropriate message. You should display your converted string using the output...
Written in MIPS assembly language If given an array which is sorted in ascending order, as...
Written in MIPS assembly language If given an array which is sorted in ascending order, as well as its length and a target value. You are asked to find the index of this target value in the array using binary search algorithm. Here is an example: array 1, 4, 6, 8, 10, 12 length 6 target 8 2 In this case, the returned result should be assigned to be 3, as array[3] is equal to target. (Note: The target will...
Write a sequence of assembly language instructions to subtract each entry of an array A of...
Write a sequence of assembly language instructions to subtract each entry of an array A of five two’s complement 16-bit binary integers from the corresponding entry of an array B of five two’s complement 16-bit binary integers and construct a third array C of two’s complement 16-bit binary integers. i.e. C[i] = A[i] - B[i]. Use the following data for the arrays A and B. A: 10, -15, 20, 4, -5 B: 25, -5, -30, 6, 10 please answer in...
Write a MIPS assembly language procedure that implements the Towers of Hanoi recursive function given the...
Write a MIPS assembly language procedure that implements the Towers of Hanoi recursive function given the following declaration: void towers(int n, char source, char dest, char spare); The function outputs a message describing each move. The source, destination, and spare poles are indicated with a character identifier of your choosing ('A', 'B', 'C' are common). Write a MIPS assembly language program that demonstrates the Towers of Hanoi procedure. Your program should ask the user for the number of disks. The...
Implement a stack in C++ using an array, not an array list. Make your stack size...
Implement a stack in C++ using an array, not an array list. Make your stack size 5 when you test it, but do not hardcode this! You should be able to change the size for testing purposes with the change of one variable. DO NOT use Stack class defined in C++ Implement the following methods in your stack class. stack() creates an empty stacks, stack s is new and empty. push(item) adds a new item to the stack s, stacks...
Write an assembly language program to define an array of 5 double words initialized to 33,44,25,72,23,11...
Write an assembly language program to define an array of 5 double words initialized to 33,44,25,72,23,11 (all decimal). Add the first three numbers together and subtract the last two numbers from the sum. Store the sum in EAX register. Display the sum by using Irvine32 library procedures or by dumping registers to the display and taking a screenshot.
Using MIPS assembly language In this program, you should define an array of 10 elements in...
Using MIPS assembly language In this program, you should define an array of 10 elements in your data segment with these values: ? = {11, 12,−10, 13, 9, 12, 14, 15,−20, 0} a. Write a function which finds the maximum value of this array. b. Write another function which calculates the summation of this array. c. Call these functions in your main program, and print the outputs of these functions to the user i. “The maximum is 15” ii. “The...
ASSEMBLY LANGUAGE x86: Reverse a string using the Run-time stack and a procedures You may want...
ASSEMBLY LANGUAGE x86: Reverse a string using the Run-time stack and a procedures You may want to use your project 4 as a starting point As in the last project we will get a string from the user put it into a buffer called 'source' (ReadString) Copy this string into a buffer called ‘destination’, in reverse order For this project you will use the run-time stack (push & pop) to reverse the string in ‘source’ You must still use indirect...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT