Sancho Jimenez
Sancho Jimenez

Reputation: 23

MMIX changing characters in input

I have an assignment where I have to take an input in MMIX and return the exact same thing, with the exception that all spaces must be newlines. I've been trying for about 2 days now and have figured out how to take input and how to output it to the console. But the second part eludes me and the assignment is due tomorrow. Here's what I have so far:

    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

Upvotes: 1

Views: 343

Answers (1)

Zartaj Majeed
Zartaj Majeed

Reputation: 510

I've inserted a loop between the calls to Fgets and Fputs

Also responding to comments

  • Data_Segment is a fixed address - where writable data starts in MMIX memory
  • Fgets returns the number of bytes read or -1 - the string is null-terminated
  • LOC #100 (0x100) is typically where the main program starts - special interrupt handlers can be placed in lower addresses
// 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

test with mmix assembler and simulator

$ mmixal space_to_newline.mms
$ echo "abc defgh ijklm nopq" | mmix space_to_newline
abc
defgh
ijklm
nopq

Upvotes: 3

Related Questions