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.

bits2strings(a, i, type)
function c = bits2strings(a, i, type)
%BITS2STRINGS returns strings for displaying memory contents
%   C = BITS2STRINGS(A, I, TYPE). Given an address A and a 1-byte value I,
%   returns strings showing the address as hex and the byte's
%   representation as bits, hex, int, ASCII and float. If the optional
%   third argument is not 'mem' the results are adjusted for the register
%   display.
%
%   Does some padding with spaces.
%
%   With no argument, returnS header strings.

%   Copyright 2008 University of Sussex and David Young

if ~nargin
    c = {'Address' 'Binary' 'Hex', 'ASCII', 'Integer', 'Float'};
    return
end

if nargin < 3
    type = 'mem';
end
if isequal(type, 'mem')
    addressfmt = '%02X';
else
    addressfmt = '%1X';     % for registers
end

ch = char(i);
if ch < 32 || ch > 126; ch = ''; end

c = { ...
    ['      ' sprintf(addressfmt, a)], ...
    dec2bin(i, 8), ...
    ['      ' sprintf('%02X', i)], ...
    ['    ' ch], ...
    sprintf('%8d', int82dble(i)), ...
    sprintf('%8.3f', f82dble(i)) ...
    };

end

Contact us at files@mathworks.com