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

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...
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...
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...
Write, specify and prove the function reverse that reverses an array in place. Take care of...
Write, specify and prove the function reverse that reverses an array in place. Take care of the unmodified part of the array at some iteration of the loop. Assume that the swap function is already proved. Note: Prototype is as below. [7 M] [CO2] void swap(int* a, int* b); void reverse(int* array, size_t len){ }
Q1: A. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO EXCHANGE 16-BIT NUMBERS B. WRITE AN ASSEMBLY LANGUAGE...
Q1: A. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO EXCHANGE 16-BIT NUMBERS B. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO SOLVE THE EQUATION Z=A+B-(C/D)+E please write the answer separately part A its own code and part B its own code this is microprocessor the ASSEMBLY LANGUAGE emu8086 should be written like this EX: mov ax,100h mov bx,200h etc
In C Programing Create a stack using an array. Define the index variable of the array...
In C Programing Create a stack using an array. Define the index variable of the array name to be stacktop. Initially set stacktop to -1. Next create two functions push and pop. Both take as input 3 items: a pointer to the stack in memory, stacktop, and maxstack. Make sure to inc stacktop by one and also remember that stacktop[0] is the bottom of the stack and by stack rule cannot be accessed until all the other items are popped....
For the PIC16F887, using the execution delay of instructions, write assembly language commands to implement a...
For the PIC16F887, using the execution delay of instructions, write assembly language commands to implement a delay of 3 seconds
In MPLAB write and compile (using the simulator) an assembly language program with the following functionality:...
In MPLAB write and compile (using the simulator) an assembly language program with the following functionality: Configures pin RA2 of the PIC24to be an output to control an attached LED. Configures pin RB13 of the PIC24 to be an input to read the value on an attached switch (this switch will connect to ground when pressed). Configures pin RB13 to use the internal pull-up resistor. After configuration, the LED will be "off" when the push-button is pressed, and "on" when...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT