如何使用x86程序集创建一个循环来打印输入值的星形行? [英] How do I create a loop using x86 assembly to print rows of stars from inputted values?

查看:84
本文介绍了如何使用x86程序集创建一个循环来打印输入值的星形行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法打印了一排星星但是当我尝试从新值打印另一行时,它会创建一个无限循环。它停在0.我想使用x86程序集,我正在使用visual studio。



示例

输入:

1

5

8

0



输出:< br $>
*

*****

********



我的尝试:



#includestdafx.h

#include< stdlib.h>



void printChar(char c);

void printStr(char * strAddr);

void printInt(int someInt);



int numItems; //项目编号

int * items; //指向项目的指针

int anItem; //将对应于所看到的星号数



int wmain(int argc,_TCHAR * argv [])

{

items =(int *)malloc(1000);



numItems = 0;

do

{

printf(输入项目%d(0表示数据结束):,numItems + 1);

scanf(%d,& anItem);

items [numItems] = anItem;

numItems ++;



} while(anItem!= 0); //用于输入和读取的数据



_asm

{



调用printNewLine



mov esi,[items] // esi指向'numItems'的开头('items'指向'numItems'的地址)



mainLoop:mov eax,[esi] // eax的内存地址为'numItems'

mov [anItem],eax //内存地址'anItem'的内存地址为'numItems'



mov eax,0

starLoop:push eax // eax first in stack

mov al,'*'

push al

call printChar //打印一张*



pop eax // eax out of stack



inc eax // eax增加1

cmp eax,[anItem ] //与'anItem'比较的eax值

jne starLoop //如果eax不等于'anItem'的值,starLoop再次启动





jmp完成



printNewLine:

push'\ r'//用于打印字符的两行。

调用printChar



push'\ n'//两行打印另一个字符。

调用printChar



ret //回到<无论我们来自哪里>



完成://做什么都不用

}





printf(按enter键退出quant \ n);

char dummy [10]; //以防万一缓冲区中的几个键

scanf(%c,dummy); //暂停。

scanf(%c,虚拟); //再次暂停

}



void printChar(char c)

{

printf( %c,c); //%c表示作为字符



}

void printStr(char * strAddr)

{

printf(%s,strAddr);

}

void printInt(int someInt)

{

printf(%d,someInt);

}

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

推荐答案

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

从什么时候开始?



看看你的代码。 项目在哪里设置为 numitmes



严肃地说,如果你想学习汇编程序,那么我强烈建议你完全抛弃c函数并使用纯汇编程序解决方案。我还建议您在考虑学习汇编程序之前需要更好的编程背景,因为您的问题历史记录并未显示您正在考虑自己的代码:您提出的问题是任何具有合理编码背景的人都应该能够解决,即使他们不知道汇编程序!

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!


这篇关于如何使用x86程序集创建一个循环来打印输入值的星形行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆