1 Replies - 496 Views - Last Post: 02 December 2019 - 10:57 AM

#1 sallu110   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-December 19

Printing half pyramid in assembly x86 language

Posted 02 December 2019 - 07:02 AM

Write a program that takes input from the user and displays the following pattern.
Sample:
Enter your input: 5
Output:
*****
****
***
**
*
I am trying to convert it to assembly! But its not working! Help please!
C++ CODE FOR Printing half pyramid in assembly
Won't all the three for loops run for x times! where x is number entered by user?
The code is running infinitely and printing nothing on the screen


#include<iostream>
using namespace std;

int main()
{
   int x;
  cout <<"Enter number:" <<endl;
  cin >> x;
  for(int i= x ;i>=1;i--)
  {
  	for (int j=x; j>i;j--)
  	{
  	 cout << " " ;	
	}
	for (int k=1; k<=i;k++)
  	{
  	 cout << "*" ;	
	}
	cout << endl;
  }
  system("pause");
}


My try
Assembly code:


include irvine32.inc
.data

printone db "enter number ",0
space db " ",0
i dd ?
k dd 1
j dd ?
sterik db "*",0

.code
main proc

 mov edx,offset printone
 call writestring
 call readstring    ; cin >> n 
 mov ecx, eax ; counter of n for outer for loop
 


L1 :


L2: 
   
   mov edx, offset space    
   call writestring
   LOOP L2
L3:
   mov edx, offset sterik
   call writestring
   LOOP L3

call crlf

loop L1


exit
main endp
end main





Is This A Good Question/Topic? 0
  • +

Replies To: Printing half pyramid in assembly x86 language

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2414
  • View blog
  • Posts: 4,546
  • Joined: 30-May 10

Re: Printing half pyramid in assembly x86 language

Posted 02 December 2019 - 10:57 AM

Step 1 - give your variables MEANINGFUL names.
Meaningless single-letter identifiers just give you word blindness.
#include<iostream>
using namespace std;

int main()
{
  int numRows;
  cout << "Enter number:" << endl;
  cin >> numRows;
  for (int row = numRows; row >= 1; row--) {
    for (int spacesOnRow = numRows; spacesOnRow > row; spacesOnRow--) {
      cout << " ";
    }
    for (int starsOnRow = 1; starsOnRow <= row; starsOnRow++) {
      cout << "*";
    }
    cout << endl;
  }
}



Step 2 - create 4 labels for each for loop, like so
f_rows_begin:
f_rows_test:
f_rows_step:
f_rows_done:



Step 3, now start to fill in the respective parts of the loop from your C code
f_rows_begin:
    ; row = numRows
f_rows_test:
    ; row >= 1
    ; jmp to f_rows_done if the condition is false
    ; Body of the for loop goes here.
f_rows_step:
    ; row--
f_rows_done:


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1