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.

savemem(machine, file, insttype)
function savemem(machine, file, insttype)
%SAVEMEM saves the current state of memory in a text file
%   SAVEMEM(MACHINE, FILE, INSTTYPE) takes a BM machine MACHINE, a string
%   FILE giving a file name, and a string INSTTYPE specifying whether
%   instruction comments should be assembly language or descriptive text.
%   The contents of memory, where it is non-zero, are saved in the file.
%
%   The file can be reloaded using LOADMEM. The file format is set out in
%   BMHELP.M.
%
%   See also: BMHELP, LOADMEM, INSTR2STRING

%   Copyright 2008 University of Sussex and David Young

nl = char(10);
[pathstr, name, ext] = fileparts(file);
savetext = ['// Brookshear Machine memory at ' datestr(now) ...
    ' in ' name ext nl];

mem = machine.memory;
eaddr = 0:2:length(mem-1);
emem = mem(1:2:end);
omem = mem(2:2:end);
ok = (emem ~= 0) | (omem ~= 0);
eaddr = eaddr(ok);
emem = emem(ok);
omem = omem(ok);

for i = 1:length(eaddr)
    a = eaddr(i); b1 = emem(i); b2 = omem(i);
    str = instr2string(insttype, b1, b2);
    if ~isempty(str)
        str = [' // ' str];
    end
    s = [dec2hex(a, 2) ': ' dec2hex(b1, 2) dec2hex(b2, 2) str nl];
    savetext = [savetext s];
end
writetext(savetext, file);
end

Contact us at files@mathworks.com