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.

memstrings(machine, rows, insttype)
function c = memstrings(machine, rows, insttype)
%MEMSTRINGS returns strings describing contents of memory as instructions
%   C = MEMSTRINGS(MACHINE, ROWS, INSTTYPE) produces a cell array of
%   strings representing the current state of the memory of the BM MACHINE
%   between the rows given by the elements of ROWS. Memory is indexed from
%   1 to 256 - i.e. rows are 1 greater than the corresponding BM addresses.
%   INSTTYPE controls whether the text is assembly language or descriptive.
%
%   With no arguments returns header strings.
%
%   If MACHINE is empty, return result for a machine whose memory has been
%   cleared.
%
%   See also: BITS2STRINGS, INSTR2STRING

%   Copyright 2008 University of Sussex and David Young

if nargin == 0
    b = bits2strings;
    c = {b{:} 'Instruction'};
    return
end

if isempty(machine)
    machine = newbm;
end
m = machine.memory;
pc = machine.pc;

r0 = rows(1);
r1 = rows(2);
c = cell(r1-r0+1, length(bits2strings)+1);
j = 0;
for i = r0:r1
    j = j+1;
    c(j, 1:end-1) = bits2strings(i-1, m(i));
    if oddaddr(pc, i) && i < length(m)  % even offsets from pc only
        c{j, end} = instr2string(insttype, m(i), m(i+1));
    else
        c{j, end} = '';
    end
end

end

Contact us at files@mathworks.com