⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util.m

📁 compiler-compiler. This is a compiler-compiler with table-driven scanner, table-driven parser.
💻 M
📖 第 1 页 / 共 2 页
字号:
%==============================================================%% File:        util.m                                          %% Author:      Nagi B. F. MIKHAIL                              %% Date:        April, 1995                                     %% Description: This file contains utility routines written in  %%              MOON's assembly to handle string operations &   %%              Input/Output.                                   %%==============================================================%%%%--------------------------------------------------------------%% getint                                                       %%--------------------------------------------------------------%% Read an integer number from stdin. Read until a non digit char% is entered. Allowes (+) & (-) signs only as first char. The% digits are read in ASCII and transformed to numbers.% Entry : none.% Exit : result -> r1%getint	align	add	r1,r0,r0		% Initialize input register (r1 = 0)	add	r2,r0,r0		% Initialize buffer's index i	add	r4,r0,r0		% Initialize sign flaggetint1	getc	r1			% Input ch	ceqi	r3,r1,43		% ch = '+' ?	bnz	r3,getint1		% Branch if true (ch = '+')	ceqi	r3,r1,45		% ch = '-' ?	bz	r3,getint2		% Branch if false (ch != '-')	addi	r4,r0,1			% Set sign flag to true	j	getint1			% Loop to get next chgetint2	clti	r3,r1,48		% ch < '0' ?	bnz	r3,getint3		% Branch if true (ch < '0')	cgti	r3,r1,57		% ch > '9' ?	bnz	r3,getint3		% Branch if true (ch > '9')	sb	getint9(r2),r1		% Store ch in buffer	addi	r2,r2,1			% i++	j	getint1			% Loop if not finishedgetint3	sb	getint9(r2),r0		% Store end of string in buffer (ch = '\0')	add	r2,r0,r0		% i = 0	add	r1,r0,r0		% N = 0	add	r3,r0,r0		% Initialize r3getint4	lb	r3,getint9(r2)		% Load ch from buffer	bz	r3,getint5		% Branch if end of string (ch = '\0')	subi	r3,r3,48		% Convert to number (M)	muli	r1,r1,10		% N *= 10	add	r1,r1,r3		% N += M	addi	r2,r2,1			% i++	j	getint4			% Loop if not finishedgetint5	bz	r4,getint6		% Branch if sign flag false	sub	r1,r0,r1		% N = -Ngetint6	jr	r15			% Return to the callergetint9	res	12			% Local buffer (12 bytes)	align%%%--------------------------------------------------------------%% putint                                                       %%--------------------------------------------------------------%% Write an integer number to stdout. Transform the number into% ASCII string taking the sign into account.% Entry : integer number -> r1% Exit : none.%putint	align	add	r2,r0,r0		% Initialize buffer's index i	cge	r3,r1,r0		% True if N >= 0	bnz	r3,putint1		% Branch if True (N >= 0)	sub	r1,r0,r1		% N = -Nputint1	modi	r4,r1,10		% Rightmost digit	addi	r4,r4,48		% Convert to ch	divi	r1,r1,10		% Truncate rightmost digit	sb	putint9(r2),r4		% Store ch in buffer	addi	r2,r2,1			% i++	bnz	r1,putint1		% Loop if not finished	bnz	r3,putint2		% Branch if True (N >= 0)	addi	r3,r0,45	sb	putint9(r2),r3		% Store '-' in buffer	addi	r2,r2,1			% i++	add	r1,r0,r0		% Initialize output register (r1 = 0)putint2	subi	r2,r2,1			% i--	lb	r1,putint9(r2)		% Load ch from buffer	putc	r1			% Output ch	bnz	r2,putint2		% Loop if not finished	jr	r15			% return to the callerputint9	res	12			% loacl buffer (12 bytes)	align%%%--------------------------------------------------------------%% putstr                                                       %%--------------------------------------------------------------%% Write a string to stdout. Write char by char until end of % string.% Entry : address of string -> r1% Exit : none.% putstr	align	add	r2,r0,r0		% Initialize output register (r2 = 0)putstr1	lb	r2,0(r1)		% Load ch from buffer	bz	r2,putstr2		% Branch if end of string (ch = '\0')	putc	r2			% Output ch	addi	r1,r1,1			% i++	j	putstr1			% Loop if not finishedputstr2	jr	r15			% Return to the caller	align%%%--------------------------------------------------------------%% putsub                                                       %%--------------------------------------------------------------% % Write a substring to stdout. Write char by char until end of % substring.% Entry : address of substring -> r1%         address of end of substring -> r3% Exit : none.% putsub	align	add	r2,r0,r0		% Initialize output register (r2 = 0)putsub1	lb	r2,0(r1)		% Load ch from buffer	putc	r2			% Output ch	ceq	r2,r1,r3		% Current pos. is end of substring ?	bnz	r2,putsub2		% Branch if end of substring	addi	r1,r1,1			% i++	j	putsub1			% Loop if not finishedputsub2	jr	r15			% Return to the caller	align%%%--------------------------------------------------------------%% getstr                                                       %%--------------------------------------------------------------%% Read a string from stdin. Read char by char until CR but do % not store CR.% Entry : address of string var. -> r1% Exit : address of string var. -> r1%getstr	align	add	r2,r0,r0		% Initialize input register (r2 = 0)	getc	r2			% Input ch	ceqi	r3,r2,10		% ch = CR ?	bnz	r3,getstr1		% branch if true (ch = CR)	sb	0(r1),r2		% Store ch in buffer	addi	r1,r1,1			% i++	j	getstr			% Loop if not finishedgetstr1	sb	0(r1),r0		% Store end of string (ch = '\0')	jr	r15			% Return to the caller	align%%%--------------------------------------------------------------%% catstr                                                       %%--------------------------------------------------------------%% Append one string to another.% Entry : address of 1st string -> r1%         address of 2nd string -> r2% Exit : address of concatenated strings -> r1%catstr	align	add	r3,r0,r0		% r3 = 0catstr1	lb	r3,0(r1)		% Load ch from 1st string	bz	r3,catstr2		% Branch if end of string (ch = `\0')	addi	r1,r1,1			% i++	j	catstr1			% Loop if not end of 1st stringcatstr2	lb	r3,0(r2)		% Load ch from 2nd string	sb	0(r1),r3		% Store ch at the end of 1st string	bz	r3,catstr3		% Branch if end of 2nd string	addi	r2,r2,1			% j++	addi	r1,r1,1			% i++	j	catstr2			% Loop if not end of 2nd stringcatstr3	jr	r15			% Return to the caller	align%%%--------------------------------------------------------------%% catsub                                                       %%--------------------------------------------------------------%% Append a substring to a string.% Entry : address of string -> r1%         address of substring -> r2%         address of end of substring -> r4% Exit : address of concatenated strings -> r1% catsub	align	add	r3,r0,r0		% r3 = 0catsub1	lb	r3,0(r1)		% Load ch from 1st string	bz	r3,catsub2		% Branch if end of string (ch = `\0')	addi	r1,r1,1			% i++	j	catsub1			% Loop if not end of 1st stringcatsub2	lb	r3,0(r2)		% Load ch from substring	sb	0(r1),r3		% Store ch at the end of 1st string	bz	r3,catsub4		% Branch if end of string ch	addi	r1,r1,1			% i++	ceq	r3,r2,r4		% Check end of substring	bnz	r3,catsub3		% Branch if end of substring	addi	r2,r2,1			% j++	j	catsub2			% Loop if not end of substringcatsub3	sb	0(r1),r0		% Store end of stringcatsub4	jr	r15			% Return to the caller	align%%%--------------------------------------------------------------%% eqstr                                                        %%--------------------------------------------------------------%% Check if string1 = string2.% Entry : address of string1 -> r1%         address of string2 -> r2% Exit : r1 = 1 (true)%        r1 = 0 (false)%eqstr	align	add	r3,r0,r0		% Initialize r3 (r3 = 0)	add	r4,r0,r0		% Initialize r4 (r4 = 0)eqstr1	lb	r3,0(r1)		% Load ch1 from 1st string	lb	r4,0(r2)		% Load ch2 from 2nd string	ceq	r5,r3,r4		% ch1 = ch2 ?	bz	r5,eqstr2		% Branch if false	ceq	r5,r3,r0		% ch1 = '\0' ?	bnz	r5,eqstr3		% Branch if true	addi	r1,r1,1			% i++	addi	r2,r2,1			% j++	j	eqstr1			% Loop if not finishedeqstr2	add	r1,r0,r0		% Return false	j	eqstr4eqstr3	addi	r1,r0,1			% Return trueeqstr4	jr	r15			% Return to the caller	align%%%--------------------------------------------------------------%% neqstr                                                       %%--------------------------------------------------------------%% Check if string1 != string2.% Entry : address of string1 -> r1%         address of string2 -> r2% Exit : r1 = 1 (true)%        r1 = 0 (false)% neqstr	align	add	r3,r0,r0		% Initialize r3 (r3 = 0)	add	r4,r0,r0		% Initialize r4 (r4 = 0)neqstr1	lb	r3,0(r1)		% Load ch1 from 1st string	lb	r4,0(r2)		% Load ch2 from 2nd string	cne	r5,r3,r4		% ch1 != ch2 ?	bnz	r5,neqstr3		% Branch if true	ceq	r5,r3,r0		% ch1 = '\0' ?	bnz	r5,neqstr2		% Branch if true	addi	r1,r1,1			% i++	addi	r2,r2,1			% j++	j	neqstr1			% Loop if not finishedneqstr2	add	r1,r0,r0		% Return false	j	neqstr4neqstr3	addi	r1,r0,1			% Return trueneqstr4	jr	r15			% Return to the caller	align%

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -