Click here to Skip to main content
15,886,069 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want the computer to analyse what I have written and print the number of asterisks corresponding to the number inputted. I can only manage a piece of code that can read what I have inputted. I can't use arrays but can use loop instructions like jnz and conditional instructions like cmp.

What I have tried:

This is the code I have tried so far:

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

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

//**************************************************************************//
// Variables defined. //
//**************************************************************************//
int numItems;
int *items; // Pointer to the items
int anItem;

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);

//**********************************************************************//
// Into assembler. //
//**********************************************************************//
__asm
{

jmp finish

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

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

ret

finish: // Do nothing
}

//**********************************************************************//
// Out of assembler. //
//**********************************************************************//
printf("press enter to quit\n");
char dummy[10]; //Just in case several keys in buffer
scanf("%c", dummy); //pause.
scanf("%c", dummy); //pause. And once more. Something weird going on.
}



//**********************************************************************//
// Prints a single character. //
// Push the char to be printed onto the stack; a First In Last Out //
// data structure. Remember, unlike C#, a char here is 1 byte in size. //
// //
// Parameters in: Push a single char onto the stack, as above. //
// Returns: Nothing. //
// Other issues: Does it preserve CPU registers eax, ebx, ecx, edx, //
// esi, edi etc.? No idea (it takes us into "C", so //
// assume not. //
//**********************************************************************//
void printChar(char c)
{
printf("%c", c); //%c means as a char

} // we don't seee the "ret" instruction unless you view the ".cod" listing
// in the "debug" folder.



//**********************************************************************//
// Print a whole string, which must end with a zero byte. //
// it takes one parameter, which is the start address of the string. //
//**********************************************************************//
void printStr(char *strAddr)
{
printf("%s", strAddr);
} // we don't seee the "ret" instruction unless you view the ".cod" listing
// in the "debug" folder.




//**********************************************************************//
// Prints a single integer . //
// Push the integer to be printed onto the stack; a First In Last Out //
// data structure. Remember, an integer here is 4 bytes in size. //
// //
// Parameters in: push a single integer onto the stack, as above. //
// Returns: Nothing. //
// Other issues: Does it preserve CPU registers eax, ebx, ecx, edx, //
// esi, edi etc.? No idea (it takes us into "C", so //
// assume not. //
//**********************************************************************//
void printInt(int someInt)
{
printf("%d", someInt);
}
Posted
Updated 19-Oct-17 4:46am

Firstly, I see no reason to use asm in there at all. Everything you are doing can be done in C. To your question, you have a do-while loop there getting the data and counting how much you have. Why can't you make another do-while loop to output the data?

One more thing - you have this tagged as unicode but you are using char everywhere. I see nothing about this that has anything to do with unicode.
 
Share this answer
 
C++
for (i = 0; items[i] != 0; i++)     // for each entry in the array
{
    int j;
    for (j = 0; j < items[i]; j++)  // for the count in this item entry 
        printChar('*');             // print the number of stars
    printChar('\n');                // new line after the stars
}
 
Share this answer
 
v2

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