Code covered by the BSD License  

Highlights from
Extended Brookshear Machine emulator and assembler

image thumbnail
from Extended Brookshear Machine emulator and assembler by David Young
Emulator and assembler for a simple computer, a teaching aid for computer science courses.

instr2string_desc(b1, b2)
function str = instr2string_desc(b1, b2)
%INSTR2STRING_DESC returns string representation of instruction
%   STR = INSTR2STRING_DESC(B1, B2) returns a string giving and informal
%   description of the instruction made of bytes B1, B2.

%   Copyright 2008 University of Sussex and David Young

% instrs = {@NOP @LOADA, @LOADI, @STORE, @MOVE, @ADDI, @ADDF, @ORBITS, @ANDBITS, ...
%     @XORBITS, @ROTBITS, @JUMP, @HALT, @LOADR, @STORER, @JUMPR};

[op, r, s, t] = decodeInstr(b1, b2);
switch op
    case 0
        if b1 == hex2dec('F') && b2 == hex2dec('FF')
            str = 'No operation';
        else
            str = '';
        end
    case 1
        str = ['Load reg ' dec2hex(r) ' from memory cell ' dec2hex(b2, 2)];
    case 2
        str = ['Load register ' dec2hex(r) ' with value ' dec2hex(b2, 2)];
    case 3
        str = ['Store register ' dec2hex(r) ' in memory cell ' dec2hex(b2, 2)];
    case 4
        str = ['Copy register ' dec2hex(s) ' to register ' dec2hex(t)];
    case 5
        str = ['Put reg ' dec2hex(s) ' + reg ' dec2hex(t) ' (ints) in reg ' dec2hex(r)];
    case 6
        str = ['Put reg ' dec2hex(s) ' + reg ' dec2hex(t) ' (floats) in reg ' dec2hex(r)];
    case 7
        str = ['Put reg ' dec2hex(s) ' OR reg ' dec2hex(t) ' in reg ' dec2hex(r)];
    case 8
        str = ['Put reg ' dec2hex(s) ' AND reg ' dec2hex(t) ' in reg ' dec2hex(r)];
    case 9
        str = ['Put reg ' dec2hex(s) ' XOR reg ' dec2hex(t) ' in reg ' dec2hex(r)];
    case 10
        str = ['Rotate register ' dec2hex(r) ' by ' num2str(t) ' bits right'];
    case 11
        if r == 0
            str = ['Jump to ' dec2hex(b2, 2)];
        else
            str = ['Jump to ' dec2hex(b2, 2) ' if reg ' dec2hex(r) ' equals reg 0'];
        end
    case 12
        str = 'Halt';
    case 13
        str = ['Load R' dec2hex(s) ' from mem addr in R' dec2hex(t)];
    case 14
        str = ['Store R' dec2hex(s) ' in mem addr in R' dec2hex(t)];
    case 15
        if r == 0 && (s == 0 || s == 2 || s == 3)
            str = ['Jump to address in register ' dec2hex(t)];
        else
            switch s
                case 0
                    test = 'equals';
                case 1
                    test = 'not equal';
                case 2
                    test = 'gt or eq.';
                case 3
                    test = 'less or eq.';
                case 4
                    test = 'greater than';
                case 5
                    test = 'less than';
                otherwise
                    test = '?';
            end
            str = ['Jump to R' dec2hex(t) ' if R' dec2hex(r) ' ' test ' R0'];
        end
end
end

Contact us at files@mathworks.com