Three comprehensive compilation of experimental training program

statement

2 Trial subroutine title the following functions

Title: receiving a plurality of a decimal value (0-9) from the keyboard, and displays the data and in decimal form.
Claim:

  1. For input of a decimal value subprogram;
  2. When the user does not input values ​​directly enter, input end;
  3. The data output is mostly decimal data, and inside the machine are calculated and hexadecimal form, needs to be installed for the value, and then outputs the result as a character string;
  4. Procedural requirements necessary message.

For example: a user input message at three values:
Please INPUT A Number:. 5
Please INPUT A Number:. 3
Please INPUT A Number:. 4
Please INPUT A Number: (no input, hit enter)
program displays the calculation results
The sum is : 12

This is not too difficult questions, the main point is how to investigate a decimal value installed for the string and then output on the display, it comes with a little study has some knowledge about the division, there is a more difficult place to control Quebec is to enter a carriage return to judge the program ends, there are many ways, because this is a decimal number, then I can right the first decimal digit characters used to save it is to determine what characters not a carriage return, then it is the end of the cycle is not the case is not moving, then this becomes a character of type int; look at the specific bar code, which is written the Notes;

#include<stdio.h>
int a[10000];  //这个是我用来存放数组的地方   因为是实验,绝对的用不到那么多空间;所以我给数组长度才是1e4
int i=0;          //用于记录数组中有多少个
char s,ans[10000];  //s是输入时要用的,ans就是最后显示的时候要用的
int main()
{
	printf("PLease input a number:");
	//fflush(stdin); //刷新标准输入缓冲区
	while(scanf("%c",&s))              //循环输入
	{
		if(s=='\n') 
			break;                          //结束标志
		else
		{
			a[i]=s;
			a[i]-=48;                   //装换为数值
		//printf("****%d",a[i]);
		}		
		fflush(stdin); //刷新标准输入缓冲区                       这个地方不能少,清楚一下缓冲区,不然会影响到后面的输入,原因请自行百度
		printf("PLease input a number:");
		i++;
	}
	_asm{
		lea esi,a        ;取地址                        
		mov ecx,i		 ;计数器
		mov eax,0		 ;累加器           记录和
test1:
		mov edx,ecx
		dec edx			 ;计数器先减去一        这个地方是因为不减一的话会多算一个,比如就一个数据,在地址中,应该就是esi代表的数值,而不是esi+1*4
		mov ebx,[esi+edx*4]
		adc eax,ebx       
		loop test1       
;求出了sum就是eax

;为了使输出变成字符串
		mov dx,0     ;结束标志        
		push dx		 ;先把0压入堆栈        字符串的结束标志是一个0,这个0是ASCII,无法从键盘上读入的
		mov ecx,eax            ;因为除法是要用到ax的,所以把eax中的sum先给ecx
chufa:
		mov bx,10    ;除数       
		mov ax,cx                ;有一些多余了,在这道题中
		shr ecx,16               ;逻辑循环右移
		mov dx,cx      ;dx:ax,被除数
		div bx         ;dx:ax ÷ bx =  ax.....dx
		movzx ecx,ax   ;把ax扩展为ecx
		add dl,30H     ;变换成字符形式
		push dx		   ;压入堆栈
		cmp ecx,0	   ;判断结束
		jne chufa
		
		mov edi,0
		lea edi,ans    ;重新使用edi,使edi指向ans字符串
tanchu:
		pop dx         ;倒序弹出,使字符串顺序正常
		mov [edi],dl 
		inc edi
		cmp dl,0
		jne tanchu
			
	}

	printf("The sum is : %s\n",ans);

	return 0;
}

I is input as above, may also be employed getchar this model, is
a = getchar () // for obtaining user input
b = getcahr () // for absorbing the transport
and the transport is not a judgment on a line the big difference is not bad, the problem is not
over!

发布了29 篇原创文章 · 获赞 13 · 访问量 2760

Guess you like

Origin blog.csdn.net/zmx2473162621/article/details/103232323