MIPS:在正确合理的列中对齐输出



我正在上一门关于计算机在较低级别的运作的课程,主要是关于到目前为止的内存管理。它的一部分是学习使用PCSPIM模拟器用于MIPS处理器的汇编语言。

我刚刚完成了有关循环的作业。为此,我必须编写一个程序,该程序提示用户输入一个数字并将数字从1到该数字打印。每10个字符打印新的线字符。假设输入将是正的。我做了那部分没问题,程序可以正常工作。

作为奖励,我被要求将输出对齐到正确的合理列中。我很难过,我不知道该怎么做。我可以在Java中弄清楚这一点,但是我刚刚开始学习集会的基础知识,在仔细研究了我到目前为止所学到的一切之后,我意识到我们还没有学会如何做到这一点。我以前曾经有这位教授,她喜欢这样做以让我们思考,看看我们是否可以在没有她帮助的情况下弄清楚。

我考虑过将所有结果放入数组中并打印出来,但我不知道如何在组装中制作数组。我还考虑使用汇编等效于IF语句,但是每次将新数字添加到数字中,并且输入不允许最大数字来编写和测试。

如果有人能告诉我如何将我的输出打印到正确的合理列中,我将非常感谢。到目前为止,这是我的代码:

###### 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

因此,任务是取决于当前值的长度和最终值的空间量。例如。用户输入1234,这意味着,1应该用3个其他空间填充,512,一个空间,1024左右。

获取输入值的长度

从1开始,然后在循环中乘以10,直到在计数循环迭代时结果大于输入值。迭代数量等于输入值的长度。让我们将其表示为l。

找到必要数量的空间

好吧,幼稚而直接的方法是使用上面的方法获得当前值的长度并从L中减去。但是,这是浪费资源,有一种简单的方法。

您知道,您总是从1开始,因此最初需要L-1其他空间。而且您知道"里程碑"数字,当前值的长度增加。因此,这就是方式:

  • L-1加载到寄存器中负责跟踪其他空间的数量
  • 10加载到负责跟踪里程碑的登记册中
  • 每次打印号码时,请执行以下操作
    • 如果数字等于里程碑,则减少附加空间的计数器,然后将里程碑乘以10。
    • 打印存储的空间数量
    • 打印电流值

最新更新