好吧,我想我开始对这个问题有了更多的了解。我创建了一个二进制文件,然后将其加载到 LC3 的模拟器中以生成一些可读的 asm。这是我得到的:
Memory:
x3000 0101010010100000 x54A0 AND R2, R2, #0 // Clear R2
x3001 0010011000010000 x2610 LD R3, x3012 // load the the value stored at x3012 into R3
x3002 1111000000100011 xF023 TRAP IN // input char
x3003 0110001011000000 x62C0 LDR R1, R3, #0 // load the value of memory R3 into R1
x3004 0001100001111100 x187C ADD R4, R1, #-4 // Add -4 to R1 to see if we have reached the end of our string
x3005 0000010000001000 x0408 BRZ x300E // Output result, end program if we run into an x0004 in the string
x3006 1001001001111111 x927F NOT R1, R1 // invert the char from the
x3007 0001001001100001 x1261 ADD R1, R1, #1 // stored string
x3008 0001001001000000 x1240 ADD R1, R1, R0 // compare with the char entered by the user
x3009 0000101000000001 x0A01 BRNP x300B // If the chars don't match grab another char from the string
x300A 0001010010100001 x14A1 ADD R2, R2, #1 // R2 is our char counter, counts the number of times we find
// the user's char in the string
x300B 0001011011100001 x16E1 ADD R3, R3, #1 // increment our memory pointer
x300C 0110001011000000 x62C0 LDR R1, R3, #0 // load the value of memory R3 into R1
x300D 0000111111110110 x0FF6 BRNZP x3004 // Jump to x3004
x300E 0010000000000100 x2004 LD R0, x3013 // Load the value of x30 into R0
x300F 0001000000000010 x1002 ADD R0, R0, R2 // Add x30 to our count to convert it to ASCII
x3010 1111000000100001 xF021 TRAP OUT // Output the count to the user
x3011 1111000000100101 xF025 TRAP HALT // Stop the program
x3012 0011000100000000 x3100 ST R0, x2F13
x3013 0000000000110000 x0030 NOP
为了帮助您开始回答第一个问题,我会做一些类似于如何使用 R3 的事情。将内存位置 x3200 加载到未使用的寄存器中,然后每次在该内存位置存储地址时递增它。示例:
LD R5, x3013 // store x3200 in the memory location x3013
.
. // if the chars match then do the following
.
STR R3, R5, #0 // Store the value of R3 into the mem of R5
ADD R5, R5, #1 // increment R5
至于您的第二个问题,它仅输出值 0-9 的原因是因为您通过添加 x30 将计数值转换为 ASCII 字符。这对于前 10 个整数非常有用,但您必须有一点创意才能添加第二个数字。
希望这会有所帮助。