Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have managed to print a row of stars but when I try to print another row from a new value, it creates an infinite loop. It stops at 0. I want to use x86 assembly and I am using visual studio.

example
input:
1
5
8
0

output:
*
*****
********

What I have tried:

#include "stdafx.h"
#include <stdlib.h>

void printChar(char c);
void printStr(char *strAddr);
void printInt(int someInt);

int numItems; //item number
int *items; // Pointer to the items
int anItem; //will correspond to number of asterisks seen

int wmain(int argc, _TCHAR* argv[])
{
items = (int *)malloc(1000);

numItems = 0;
do
{
printf("Enter item %d (0 means end of data): ", numItems + 1);
scanf("%d", &anItem);
items[numItems] = anItem;
numItems++;

} while (anItem != 0); //for data to be input and read

_asm
{

call printNewLine

mov esi, [items] //esi points to start of 'numItems' ('items' points to address of 'numItems')

mainLoop: mov eax, [esi] //eax has memory address of 'numItems'
mov [anItem], eax //memory address of 'anItem' has the memory address of 'numItems'

mov eax, 0
starLoop: push eax //eax first in stack
mov al, '*'
push al
call printChar //Prints a single *

pop eax //eax out of stack

inc eax //eax increases by 1
cmp eax, [anItem] //value of eax compared to 'anItem'
jne starLoop //if eax doesn't equal value of 'anItem', starLoop starts again


jmp finish

printNewLine:
push '\r' // Two lines to print a character.
call printChar

push '\n' // Two lines to print another character.
call printChar

ret // And back to <wherever we came from>

finish : // Do nothing
}


printf("press enter to quit\n");
char dummy[10]; //Just in case several keys in buffer
scanf("%c", dummy); //pause.
scanf("%c", dummy); //pause again.
}

void printChar(char c)
{
printf("%c", c); //%c means as a char

}
void printStr(char *strAddr)
{
printf("%s", strAddr);
}
void printInt(int someInt)
{
printf("%d", someInt);
}
Posted
Updated 21-Oct-17 1:04am
v2
Comments
Richard MacCutchan 21-Oct-17 6:52am    
Have you just created a new account to repeat what you already posted a couple of days ago? and we gave you enough suggestions to solve it. The main suggestion being to stop using assembler and stick to C. Once you are a C expert then you may try switching to assembler.
[no name] 21-Oct-17 6:56am    
Stupid comment "stop using assembler and stick to C"!
There are People in the world who like to learn....
Richard MacCutchan 21-Oct-17 7:12am    
If you do not have anything useful to add then please don't fill the forums with stupid comments.
[no name] 21-Oct-17 7:17am    
Same to you
Richard MacCutchan 21-Oct-17 8:10am    
Please, either grow up or find some other forum to annoy people.

1 solution

mov esi, [items] //esi points to start of 'numItems' ('items' points to address of 'numItems')
Since when?

Look at your code. Where does items get set to numitmes?

In all seriousness, if you want to learn assembler, then I'd strongly suggest you dump the c stuff completely and use a "pure" assembler solution. I'd also suggest that you need a much better programming background before you even think of learning assembler as your question history does not show that you are thinking about your own code: you are asking questions that anyone with a reasonable background in coding should be able to work out, even if they don't know assembler!
 
Share this answer
 
Comments
[no name] 21-Oct-17 7:09am    
I didn't know this turned into some kind of place to me people feel stupid? Thanks for the lectures but blaming me for not being able to understand it the way you do is a little mean. I had previously used items as a pointer to numItems. Thank you for your advice. I didn't mean to get any of you angry but all I seem to get are answers making me feel stupid. Sorry for wasting your time and obviously showing how "stupid" I am. (C was only used as a starting point)
[no name] 21-Oct-17 7:15am    
"I didn't know this turned into some kind of place to me people feel stupid?"
It is not like this, usually....
Stay tuned for a while
Avoid being thin skinned
... It was the same for me also ;)

It is a great forum to get help. Just filter out some of the noise :-)
[no name] 21-Oct-17 7:19am    
Thanks for the advice. I feel like I'm getting the same replies from the same people who don't like me clearly.
[no name] 21-Oct-17 7:26am    
Same experience here. Just skip some of the comments ;)
OriginalGriff 21-Oct-17 7:26am    
Stop and think about what you are asking in the form of questions instead of jumping on your high horse and assuming we think you are an idiot, and that we are getting angry. We don't, and we aren't (or I am not at least): but your questions do not show much evidence of actual thinking, much less a reasonable degree of computer experience. If you had that, you wouldn't be asking these questions! And when we see the "same damn code" for the fourth or fifth time, we do get the feeling that someone is taking the Mickey.

Assembly language is complicated: not because there is a lot too it, but quite the reverse - it's horribly complicated because there isn't much to it: you have to handle everything yourself, with the exception of OS primitives to interact with the hardware and thus the user. Which is why you are faffing with C to provide some of the interactions, just to make your life easier. But they aren't designed for that, so unless you protect your code from them you run into problems like the register corruption you had yesterday.

Stop using C as a "carrier" to insulate yourself from the hardware and use a "proper" assembler: that way people don't assume you are trying to learn C at the same time and get diverted. You want to learn assembler? Start here:

https://www.youtube.com/watch?v=9e1ER2o83N0

(Normally I avoid YouTube tutorials like the plague, but this one is actually helpfull) and then get a copy of this:

https://www.amazon.co.uk/Assembly-Language-X86-Processors-Irvine/dp/013602212X

It will teach you far more than your current approach seems to! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900