我正在上一门关于计算机如何在较低级别上运行的课程,到目前为止主要是关于内存管理。其中一部分是学习使用 MIPS 处理器的 PCSPIM 模拟器用汇编语言编写。
我刚刚完成了一个关于循环的作业。为此,我必须编写一个程序,提示用户输入一个数字并打印从 1 到该数字的数字。每 10 个字符打印一次换行符。假设输入为正。我做了那部分没问题,程序运行良好。
但是作为奖励,我被要求将输出对齐到右对齐列。我被难住了,我不知道该怎么做。我可以用 Java 解决这个问题,但我刚开始学习汇编的基础知识,在回顾了迄今为止我在课堂上学到的所有内容之后,我意识到我们根本没有学会如何做到这一点。我以前有过这位教授,她喜欢这样做让我们思考,看看我们是否能在没有她帮助的情况下解决这个问题。
我曾考虑将所有结果放入一个数组并打印出来,但我不知道如何在 Assembly 中制作数组。我也考虑过使用与 IF 语句等效的 Assembly,但每次将新数字添加到数字时,我都必须编写和测试一个,并且输入没有最大数量。
如果有人能告诉我如何将我的输出打印到右对齐列中,我将不胜感激。到目前为止,这是我的代码:
###### Begin Text Section ######
.text
.globl __start
__start: # execution starts here
la $a0,prompt # load address of prompt into a0
li $v0,4 # load instruction number to display a string into v0
syscall # system call to print the prompt string
li $v0,5 # load call code number to read first integer -> v0
syscall # system call to read first integer and store in v0
move $t0,$v0 # move integer from v0 -> t0 for safe keeping
# t0 holds the Final Integer
addi $t1,1 # initialize t1 to 1
# t1 is the counter
addi $t2,1 # initialize t2 to 1
# t2 is the lineCounter
addi $t3,10 # initialize t3 to 10
# t3 is the Sentinel
la $a0,endl # load the new line character into a0
li $v0,4 # load the call code number to display the string into v0
syscall # system call to print the new line character
move $a0,$t1 # move t1 -> a0 for display
li $v0,1 # load call code number to display integer into v0
syscall # system call to print t1 as largest
la $a0,space # load address of prompt into a0
li $v0,4 # load instruction number to display a string into v0
syscall # system call to print the prompt string
WHILE: ble $t0,$t1,ENDWHILE # IF counter > final integer BREAK to ENDWHILE
add $t1,$t1,1 # increment counter
move $a0,$t1 # move t1 -> a0 for display
li $v0,1 # load call code number to display integer into v0
syscall # system call to print t1 as largest
la $a0,space # load address of prompt into a0
li $v0,4 # load instruction number to display a string into v0
syscall # system call to print the prompt string
add $t2,1 # increment lineCounter
beq $t2,$t3,NEWLINE # IF lineCounter = 10 BREAK to NEWLINE
j WHILE # go around the loop again
NEWLINE: la $a0,endl # display new line
li $v0,4
syscall
addi $t2,-10 # re-initialize t2 to 0
j WHILE # jump to WHILE
ENDWHILE: la $a0,endl # display new line
li $v0,4
syscall
li,$v0,10 #End Program
syscall
##### End Text Section #####
##### Begin Data Section ######
.data
prompt: .asciiz "Please enter a posetive integer: " # prompt for the Input
space: .asciiz " " # Space Character
endl: .asciiz "\n" # New Line Character
Example of output as the code is now:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
...92 93 94 95 96 97 98 99 100
Example of how output should look:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
... 92 93 94 95 96 97 98 99 100