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.

string2val(type, inp)
function v = string2val(type, inp)
%STRING2VAL returns value from text entry in display
%   V = STRING2VAL(TYPE, INP) takes an integer TYPE indicating the column
%   of the memory display into which the string INP has been typed, and
%   carries out the appropriate conversion of the string into a value,
%   which is returned as a uint32.

%   Copyright 2008 University of Sussex and David Young

try
    switch type
        case 2  % binary
            v = bitand(uint32(bin2dec(inp)), hex2dec('ff'));
        case 3  % hex
            v = bitand(uint32(hex2dec(inp)), hex2dec('ff'));
        case 4  % ascii
            v = uint32(inp(end));
        case 5  % signed int
            v = dble2int8(str2double(inp));
        case 6  % float
            v = dble2f8(str2double(inp));
        otherwise
            throw(MException('bmachine:string2val:syserror', ['Unknown value input type ' num2str(type)]));
    end
catch err
    if isequal(err.identifier, 'bmachine:string2val:syserror')
        rethrow(err)
    else
        throw(MException('bmachine:string2val:inputerror', ['Incorrect input: ' inp]));
    end
end

end

Contact us at files@mathworks.com