0

我有一个任务,我必须在 MMIX 中输入并返回完全相同的内容,但所有空格都必须是换行符。我已经尝试了大约 2 天,并且已经弄清楚如何获取输入以及如何将其输出到控制台。但是第二部分让我望而却步,明天就要交作业了。这是我到目前为止所拥有的:

    LOC Data_Segment         % Sets data storage location
    GREG    @                % Reserves a new global register
Wordbuffer  BYTE    0        % New Byte for incoming words

    LOC     Wordbuffer+5     % After the buffer
Arg OCTA    Wordbuffer      
    OCTA    21               % Sets max word length
    LOC    #100              % Code section

Main    LDA    $255,Arg   % Loads the buffer into the global register
    TRAP    0,Fgets,StdIn    % Gets input
    LDA     $255,Wordbuffer  % Puts input in global register
    TRAP    0,Fputs,StdOut   % Prints inputted word
    TRAP    0,Halt,0         % Stops program
4

1 回答 1

1

我在对 Fgets 和 Fputs 的调用之间插入了一个循环

也回复评论

  • Data_Segment 是一个固定地址 - 可写数据在 MMIX 内存中开始
  • Fgets 返回读取的字节数或 -1 - 字符串以 null 结尾
  • LOC #100 (0x100) 通常是主程序开始的地方 - 特殊的中断处理程序可以放在较低的地址
// space_to_newline.mms
              LOC     Data_Segment     % Sets data storage location
              GREG    @                % Reserves a new global register
Wordbuffer    BYTE    0                % New Byte for incoming words
              LOC     Wordbuffer+5     % After the buffer
Arg           OCTA    Wordbuffer
              OCTA    21               % Sets max word length
              LOC     #100             % Code section

Main          LDA     $255,Arg         % Loads the buffer into the global register
              TRAP    0,Fgets,StdIn    % Gets input
// loop over the string in Wordbuffer
// check if each character is a space
// replace with newline (0xa) if so
nl            GREG    #0a              newline constant
              LDA     $0,Wordbuffer    load base address
              SET     $1,0             initialize index to zero

0H            LDBU    $2,$0,$1         load byte in $2
              BZ      $2,2F            done if null byte
              CMP     $255,$2,' '      check if space
              PBNZ    $255,1F          skip character if not space
              STBU    nl,$0,$1         fallthru to replace with newline
1H            INCL    $1,1             increment index
              JMP     0B               loop back
2H            LDA     $255,Wordbuffer  % Puts input in global register
              TRAP    0,Fputs,StdOut   % Prints inputted word
              TRAP    0,Halt,0         % Stops program

使用 mmix 汇编器和模拟器进行测试

$ mmixal space_to_newline.mms
$ echo "abc defgh ijklm nopq" | mmix space_to_newline
abc
defgh
ijklm
nopq
于 2021-04-21T20:33:50.403 回答