i have a home work on the LC-3 assemply language, it is asking me to write a sub
ID: 653598 • Letter: I
Question
i have a home work on the LC-3 assemply language, it is asking me to write a subroutine and i need helo on it.
it is:
write an LC-3 assembly language subroutine, named READS, which will allow a user to input and store in the memory a character string (length <= 9) until thenewline character (ASCII decimal 10) is typed. the beginning address of the memory location where the string is to be stored is passed to the subroutine in register R1.
write another subroutine, REVERSES, to revers the entered string.
write another subroutine, REMOVEC, to remove a single character from a string
write a program that:
- calls the subroutine READS to input a string and then uses PUTS to display it on the screen
-calls the subroutine REVERSE to reverse the string and then uses PUTS to display on the screen
- asks for a single character. the program should remove that character,if exists, from the above string and then uses PUTS to display the string without that character
Explanation / Answer
module LC3.Parser ( parseProgram ) where import Char (ord, toUpper) import Data.Bits ((.|.)) import Data.List (elemIndex) import qualified Data.Map as M import Text.ParserCombinators.Parsec hiding (label, labels) import Text.ParserCombinators.Parsec.Token import LC3.Types -- Given a filename of an LC-3 assembly program, will parse it and return -- a Program object or an error. parseProgram = runParser program (0, M.empty) -- No read instance for binary literals, so here's a thing. fromBinaryString (x:xs) | x == '-' = -fromBinaryString xs | otherwise = foldl go 0 (x:xs) where go a b | b `elem` "01" = 2 * a + (ord b - ord '0') | otherwise = error "unexpected input in binary string" -- Given a string with characters corresponding to fields in a bit vector, -- returns the int with the proper bits set. str2bits :: String -> String -> Int str2bits table (c:cs) = case elemIndex c $ reverse table of Just i -> 2^i .|. str2bits table cs Nothing -> str2bits table cs str2bits _ _ = 0 -- Updates the Map t with all the labels in ls mapped to the address adr. -- Upon finding an already existing label, returns a left value with an error -- message. Also increments the current address by the width of the instruction. updateSymbolTable :: Statement -> AddressTable -> Either String AddressTable updateSymbolTable statement table = let newAddress = fst table + width statement newTable = go (labels statement) (address statement) (snd table) go [] _ tbl = Right tbl go (l:ls) adr tbl | l `M.member` tbl = Left $ "label " ++ l ++ " already used" | otherwise = case go ls adr tbl of Right newTbl -> Right (M.union (M.insert l adr tbl) newTbl) Left err -> Left err in case newTable of Right tbl -> Right (newAddress, tbl) Left err -> Left err type AddressTable = (Int, SymbolTable) -- Our lexer for LC-3 assembly. Gives us useful methods for dealing with -- reserved words, case sensitivity, and more! lexer :: TokenParser AddressTable lexer = makeTokenParser LanguageDef { commentStart = "" , commentEnd = "" , commentLine = ";" , nestedComments = False , identStart = letter oneOf "_" , identLetter = alphaNum oneOf "_" , opStart = oneOf "" , opLetter = oneOf "" , reservedNames = opcodes ++ branches ++ traps ++ pseudoOps ++ directives , reservedOpNames = [] , caseSensitive = False } m_charLiteral = charLiteral lexer m_comma = comma lexer m_commaSep = commaSep lexer m_identifier = identifier lexer m_lexeme = lexeme lexer m_reserved = reserved lexer m_stringLiteral = stringLiteral lexer m_symbol = symbol lexer m_whiteSpace = whiteSpace lexer -- Parser for an entire LC-3 assembly program. program :: CharParser AddressTable Program program = do pRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.