[考试] 2007年度上学期 计算系统基础(A卷上机)

本文提供了一个LC-3汇编语言程序实例,该程序接收用户输入的单个数字并输出相应数量的字母表开头字母。此外,还提供了一个C语言程序示例,该程序能够移除字符串中的所有数字字符。

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

[quote]要求:
[list][*]1. 在D盘新建一个文件夹,命名为exam_071251***(即你的学号);
[*]2. 两道题目均保存在这个目录下;
[*]3. 交卷时,退出LC-3模拟器和VC,即可离开考场;
[*]4. 不允许使用U盘;
[*]5. 请关闭手机等通讯设备。[/list]

请仔细审题。

1. (10%) LC-3 assembly language program:
Inputs a single digit and prints out that number of letters from the beginning of the alphabet. For example, if the user enters "4", "abcd" is printed to the screen, followed by a line feed.

[code]letter | ASCII
'a' | x0061
line feed | x000A
'0' | x0030[/code]

(LC-3汇编程序设计:用户输入一位十进制数[i]n[/i],程序输出字母表开头的前[i]n[/i]个字母。例如:如果用户输入“4”,则输出“abcd”,并换新行。)

2. (10%) C程序设计:
编写一个函数 void delnum( char* s ),将s所指的字符串中的数字字符删除。
编写main函数:提示用户输入一个字符串,调用delnum函数,并输出调用后的结果。
例如:输入为“abcde123fg”,则输出为:“abcdefg”。[/quote]

解答:
1. test1.asm
[code]; Written by RednaxelaFX, 2008/01/09
;
; Use R2 for 'a'
; Use R4 for iteration count
; Use R5 for max iteration count
; Use R6 for ASCII to int conversion
;
.ORIG x3000
INIT LD R2, ASCIIa
LD R6, A2I
;
INPUT ; get input from keyboard
TRAP x23
ADD R5, R0, R6 ; get digit from ASCII
;
; initialze R4 for loop
AND R4, R4, #0
OUTPUT ; print ('a' + R4)
ADD R0, R2, R4
TRAP x21
; increment R4
ADD R4, R4, #1
; check iteration condition
NOT R3, R4
ADD R3, R3, #1
ADD R3, R5, R3 ; R3 = R5 - R4
BRp OUTPUT
; end of loop
; print line feed
LD R0, ASCIILineFeed
TRAP x21
HALT
ASCIILineFeed .FILL x000A
ASCIIa .FILL x0061
A2I .FILL x-30
.END[/code]

2. test2.c
/*
* Written by RednaxelaFX, 2008/01/09
*/

#include <stdio.h>
#include <string.h>

void delnum( char* s ) {
int origLen;
char* tempStr;
char* origPtr;
char* tempPtr;

origLen = strlen( s );
tempStr = ( char* ) malloc( ( origLen + 1 ) * sizeof( char ) );
if ( NULL == tempStr) {
printf( "Memory allocation failure.\n" );
exit( 1 );
}

origPtr = s;
tempPtr = tempStr;

strncpy( tempStr, s, origLen );
*( tempStr + origLen ) = '\0';

while ( *tempPtr ) {
if ( !( ( *tempPtr >= '0' ) && ( *tempPtr <= '9' ) ) ) {
*origPtr = *tempPtr;
++origPtr;
}
++tempPtr;
}
*origPtr = '\0';

free( tempStr );
}

void main( ) {
char input[50];

printf( "Enter a string: " );
scanf( "%s", input );

delnum( input );

printf( "Output: %s\n", input );
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值