打印寄存器中包含的数字

发布于 2024-10-08 20:05:38 字数 293 浏览 0 评论 0原文

我正在学习 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

我做错了什么?

I'm learning MMIX so I tried making a simple program to add one to itself and print the result. Unfortunately it doesn't print anything. Here is my program:

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

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦纸 2024-10-15 20:05:38

在看到代码此处后,我最终弄清楚了。我必须首先创建一个字节,然后将寄存器的值存储到该字节中。然后通过打印该字节,我得到 ADD y,n,1 的结果。

I ended up figuring it out after seeing the code here. I had to first create a byte, then store the value of the register into the byte. Then by printing out that byte, I get the result of ADD y,n,1.

向地狱狂奔 2024-10-15 20:05:38

罗伯特自己的回复中的链接已损坏。对此的解释也并不令人满意。

主要问题是 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

The link in Robert's own response is broken. Also the explanation is unsatisfactory.

The main issue is there is no printf in MMIX assembly. So you can't just print a number directly. It needs to be converted to a string for Fputs to work.

Once you know this the solution is easy. The challenge is to code it in MMIX. The program below handles one unsigned number.

// 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文