Printing a number contained in a register

524 views Asked by At

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?

2

There are 2 answers

0
Zartaj Majeed On

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
0
Robert On

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.