021 字符数组

本文介绍如何使用字符数组创建一个简单的文本编辑器,包括输入文本行直到遇到空行,然后逐个字符显示所有文本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/***********************021 字符数组**************************
 * 以字符数组为基础做简单的文版编辑器,该程序出入文本行直至遇到
 * 一个空行为止,而后每次一个字符重新显示各行。 
 * 出语言精彩编程百例 第21 */

#include<stdio.h>
#define MAX 100
#define LEN 80

void main()
{
	char text[MAX][LEN];
	register int t,i,j;

	//逐行输入字符串
	for(t=0;t<MAX;t++)
	{
		printf("%d:",t);
		gets(text[t]);
		if(!text[t][0])
			break;//空行退出
	}
	//按行,逐个字符输出字符串
	for(i=0;i<t;i++)
	{
		for(j=0;text[i][j];j++)
			putchar(text[i][j]);
		putchar('\n');
	}
}

对应的还没完全搞明白的汇编:

	.file	"021.c"
	.def	___main;	.scl	2;	.type	32;	.endef
	.text
LC0:
	.ascii "%d:\0"
	.align 2
.globl _main
	.def	_main;	.scl	2;	.type	32;	.endef
_main:
	pushl	%ebp
	movl	%esp, %ebp
	movl	$8024, %eax           #  数组8000个char,占8000,还有3个int占12,变量是从-8(%ebp)开始分配的, ebp到-8(ebp)之间是?,二位数组和3个int变量之间空了4,不清楚目的 
	call	__alloca              #  
	andl	$-16, %esp            #  栈空间16位对齐,esp的低四位置为0,
	movl	$0, %eax              #  eax =0
	movl	%eax, -8012(%ebp)     #  -8012(%ebp) =0
	movl	-8012(%ebp), %eax     #  
	call	__alloca              # 在 ___main前第二次分配栈空间,并且分配0b,没看懂
	call	___main
	movl	$0, -8016(%ebp)       # t=0,text[MAX][LEN] :-8008(%ebp);t:-8016(%ebp);i:-8020(%ebp); j:-8024(%ebp)
L4:
	cmpl	$99, -8016(%ebp)      # t<MAX
	jle	L7
	jmp	L5
L7:
	subl	$8, %esp              # printf("%d:",t);  
	pushl	-8016(%ebp)
	pushl	$LC0
	call	_printf
	addl	$16, %esp
	subl	$12, %esp             # gets(text[t]); 
	leal	-8008(%ebp), %edx     # edx=&text 
	movl	-8016(%ebp), %eax     # eax=t
	sall	$2, %eax              # eax=eax*4=t*4
	addl	-8016(%ebp), %eax     # eax=eax+t=t*5
	sall	$4, %eax              # eax=eax*16=t*80
	leal	(%eax,%edx), %eax     # eax=eax+edx=t*80+&text
	pushl	%eax
	call	_gets
	addl	$16, %esp
	movl	-8016(%ebp), %eax     # eax=t
	sall	$2, %eax              # eax=eax*4=t*4
	addl	-8016(%ebp), %eax     # eax=eax+t=t*5
	sall	$4, %eax              # eax=eax*16=t*80
	leal	-8(%ebp), %edx        # edx=&text[MAX][LEN]
	addl	%edx, %eax            # eax=&text[MAX][LEN]+t*80
	subl	$8000, %eax           # eax=&text[MAX][LEN]+t*80-8000=&text[t][0]
	cmpb	$0, (%eax)            # if(!text[t][0])
	jne	L6
	jmp	L5
L6:
	incl	-8016(%ebp)           # t++
	jmp	L4
L5:
	movl	$0, -8020(%ebp)       # i=0
L9:
	movl	-8016(%ebp), %eax     # eax=t
	cmpl	%eax, -8020(%ebp)     # i<t
	jl	L12
	jmp	L3
L12:
	movl	$0, -8024(%ebp)       # j =0
L13:
	movl	-8020(%ebp), %eax     # eax =i
	sall	$2, %eax              # eax=i*4
	addl	-8020(%ebp), %eax     # eax=i*5
	sall	$4, %eax              # eax=i*80
	addl	-8024(%ebp), %eax     # eax=i*80+j
	leal	-8(%ebp), %edx        # edx=&text[MAX][LEN]
	addl	%edx, %eax            # eax=&text[MAX][LEN]+i*80+j
	subl	$8000, %eax           # eax=&text[MAX][LEN]+i*80+j -8000 = &text[i][j]  
	cmpb	$0, (%eax)            # text[i][j]!=0
	jne	L16
	jmp	L14
L16:
	subl	$12, %esp             # putchar(text[i][j]);
	movl	-8020(%ebp), %eax     # eax =i
	sall	$2, %eax              # eax=i*4
	addl	-8020(%ebp), %eax     # eax=i*5
	sall	$4, %eax              # eax=i*80
	addl	-8024(%ebp), %eax     # eax=i*80+j
	leal	-8(%ebp), %edx        # edx=&text[MAX][LEN]
	addl	%edx, %eax            # eax=&text[MAX][LEN]+i*80+j
	subl	$8000, %eax           # eax=&text[MAX][LEN]+i*80+j -8000 = &text[i][j]
	movsbl	(%eax),%eax           # eax=&eax
	pushl	%eax
	call	_putchar
	addl	$16, %esp
	incl	-8024(%ebp)
	jmp	L13
L14:
	subl	$12, %esp
	pushl	$10
	call	_putchar
	addl	$16, %esp
	incl	-8020(%ebp)
	jmp	L9
L3:
	leave
	ret
	.def	_putchar;	.scl	2;	.type	32;	.endef
	.def	_gets;	.scl	2;	.type	32;	.endef
	.def	_printf;	.scl	2;	.type	32;	.endef



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值