0

私はコンピューターが低レベルでどのように機能するかについて、主にメモリ管理についてのクラスを取っています。その一部は、MIPS プロセッサ用の PCSPIM シミュレータを使用してアセンブリ言語で記述することを学習することです。

ループに関する課題を終えたところです。そのために、ユーザーに数字を入力するように促し、1 からその数字までの数字を出力するプログラムを作成する必要がありました。改行文字を 10 文字ごとに出力します。入力は正であると想定されます。私はその部分を問題なく実行しました。プログラムは正常に動作します。

ただし、おまけとして、出力を右揃えの列に揃えるように求められました。私は困惑しています、私はそれを行う方法がわかりません。私はJavaでそれを理解することができましたが、アセンブリの基本を学び始めたばかりで、クラ​​スでこれまでに学んだことをすべて調べた後、これを行う方法をまったく学んでいないことに気付きました. 私は以前にこの教授に会ったことがあります.

すべての結果を配列に入れて出力することを考えましたが、Assembly で配列を作成する方法がわかりません。Assembly の IF ステートメントに相当するものを使用することも検討しましたが、数値に新しい数字が追加されるたびに 1 つを記述してテストする必要があり、入力に許可される最大数はありません。

私の出力を右揃えの列に印刷する方法を誰かが教えてくれたら、とても感謝しています。これまでの私のコードは次のとおりです。

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

1 に答える 1