【问题标题】:Printing a number contained in a register打印包含在寄存器中的数字
【发布时间】:2010-12-21 20:48:28
【问题描述】:

我正在学习 MMIX,所以我尝试制作一个简单的程序来添加一个并打印结果。不幸的是,它不打印任何东西。这是我的程序:

n    IS $4 
y    IS $3
t    IS $255
     LOC #100
Main SET n,1 %let n = 1
     ADD y,n,1 %add 1 to n and store the result in y
     LDA t,y 
     TRAP 0,Fputs,StdOut
     TRAP 0,Halt,0

我做错了什么?

【问题讨论】:

    标签: taocp mmix


    【解决方案1】:

    在看到代码here 后,我最终弄明白了。我必须先创建一个字节,然后将寄存器的值存储到字节中。然后通过打印出那个字节,我得到了 ADD y,n,1 的结果。

    【讨论】:

      【解决方案2】:

      罗伯特自己的回复中的链接已损坏。解释也不尽如人意。

      主要问题是 MMIX 程序集中没有 printf。所以你不能直接打印一个数字。需要将其转换为字符串,Fputs 才能工作。

      一旦您知道这一点,解决方案就很简单了。挑战在于在 MMIX 中对其进行编码。下面的程序处理一个无符号数。

      // printnum.mms
      // run with MMIX simulator or visual debugger: https://mmix.cs.hm.edu
      
      n        IS $4
      y        IS $3
      t        IS $255
      
      // a register for extracting a digit
      digit    IS $5
      // a 16-byte buffer for the converted string
      buf      OCTA 0
      
               LOC #100
      Main     SET n,1 %let n = 1
               ADD y,n,1 %add 1 to n and store the result in y
      
      // convert y to ascii digits and store in buf
      
               GETA t,buf+16
      // divide and set digit to the remainder register rR
      1H       DIV y,y,10
               GET digit,rR
      // convert digit to ascii character
               INCL digit,'0'
      // fill buffer from the end
               SUB t,t,1
               STBU digit,t,0
      // loop back to 1H for more digits
               PBNZ y,1B
      
      // print the converted string
      // this works because string offset is already in register $255
               TRAP 0,Fputs,StdOut
      
               TRAP 0,Halt,0
      

      【讨论】:

        猜你喜欢
        • 2012-12-15
        • 2015-12-30
        • 1970-01-01
        • 2017-12-04
        • 2011-07-22
        • 2021-03-29
        • 1970-01-01
        • 2017-07-30
        • 1970-01-01
        相关资源
        最近更新 更多