0

I'm stuck on converting hex or decimal to 16-bit binary,and I don't know how to start. Here is the tasks I need to do:

Decimal Conversion Task: Add code that displays the value in R3 to the console as a binary string. It should convert R3 one bit at a time, to ASCII. The displayed format should be exactly of the form: Answer: 0000000000000000

• Your code should be placed in the file where it says “Put your code for part 3 here.”

• Your code must start with a label named “PrintResult”.

• On entry to your code, R3 will contain the value to output to the console.

• At the end of your code, it must unconditionally branch to a label called “ReadString”.

Hint: You can check if the leftmost bit of a number is one, by checking if it is negative. To shift all of the bits one position to the left, multiply the number by two.

Hex Conversion: Add code that parses a hexadecimal string and converts it to a 16 bit two’s compliment number. Assume that all of the numbers will fit in 16 bits.

• Your code should be placed in the file where it says “Put your code for part 4 here.”

• Your code must start with a label named “ParseHex”.

• On entry to your code, R1 will be -1 if the final result should be negated, otherwise it will be 0 R2 will contain the address of a null terminated string containing the digits to parse

• On exit from your code, R3 must contain the converted number, unless there was an invalid string.

• The code should branch to “PrintResult” after it is done, unless it encounters an invalid string, in which case it should branch to “ParseError”.

Hint: xABCD = (((xA · 16 + xB) · 16 + xC) · 16 + xD, also 16x = 2 · (2 · (2 · (2 · x)))

Here is my program:

.ORIG x3000

ReadString
        LEA     R0, Prompt
        PUTS                    ; Display a prompt requesting input
        LD      R1, BuffLen     ; Set R1 to length of input buffer
        LEA     R2, BuffPtr     ; Set R2 to address of input buffer in memory
        BRnzp   ReadCore        ; Goto the core input code
BuffPtr .BLKW 10                 ; The memory reserved for the input buffer
Prompt  .STRINGZ "Enter a number: " ; The prompt string
BuffLen .FILL 10                 ; The length of the input buffer

;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Insert your code for ReadCore from part 1 here.
ReadCore
    ADD R1,R1,#-1
    BRz Full
    GETC 
    OUT 
    ADD R5,R0,#-13
    BRz Full
    LD R3,lowerCase
    ADD R6,R3,R0
    BRn Store
    LD R3, UpperCase
    ADD R0,R0,R3 

Store
    STR R0,R2,#0
    ADD R2,R2,#1
    BRnzp ReadCore
Full 
    AND R0,R0,#0
    STR  R0,R2,#0
    BRnzp ProcessInput
lowerCase .FILL x-61
UpperCase .FILL x-20

;;; Your code from part 1 here ends here.
;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

ProcessInput
        LEA     R2, BuffPtr     ; Set R2 to address of buffer in memory
        BRnzp   ProcessInputCore

;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Insert your code for ProcessInputCore from part 2 here.
ProcessInputCore
    AND R1,R1,#0
    LD R4,Sign
    LDR R5,R2,#0
    ADD R4,R4,R5
    BRnp NotN
    ADD R1,R1,#-1
    ADD R2,R2,#1
NotN    
    ADD R1,R1,#0
    LD R6,pound
    LDR R5,R2,#0
    ADD R6,R6,R5
    BRnp ParseH
    ADD R2,R2,#1
    BRnzp ParseDecimal

ParseH
    LDR R5,R2,#0
    LD R6,ex
    ADD R6,R6,R5
    BRnp Skip
    ADD R2,R2,#1
Skip    BRnzp ParseHex

pound .FILL #-35
ex    .FILL #-120   
Sign .FILL #-45

;;; Your code from part 2 here ends here.
;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

ParseDecimal
        LD      R5, NegZERO        
        LD      R6, NegNINE        
        AND     R3, R3, #0      ; Set R3 to 0
DecimalLoop
        LDR     R4, R2, #0      ; Put next digit into R4
        BRz     DoneParsingDecimal  ; If it is the null terminator, done parsing

        ;; Confirm that it is a valid digit
        ADD     R0, R4, R5      ; Compare to "0"
        BRn     ParseError      ; If less than "0", not a digit
        ADD     R0, R4, R6      ; Compare to "9"
        BRp     ParseError      ; If greater than "9", not a digit

        ;; Multiply R3 by 10
        ADD     R7, R3, R3      ; R7 = 2*R3
        ADD     R3, R7, R7      ; R3 = 4*R3
        ADD     R3, R3, R3      ; R3 = 8*R3
        ADD     R3, R3, R7      ; R3 = 8*R3 + 2*R3 = 10*R3

        ;; Add new digit to R3
        ADD     R4, R4, R5      ; Make binary
        ADD     R3, R3, R4      ; Add new digit
        ADD     R2, R2, #1      ; Increment memory pointer
        BRnzp   DecimalLoop

DoneParsingDecimal
        ;; Check if we need to negate the result for the minus sign
        ADD     R1, R1, #0       ; If R1 is not -1, skip negation of R3
        BRzp    NotNegativeDecimal 
        NOT     R3, R3
        ADD     R3, R3, #1      ; Negate R3
NotNegativeDecimal
        BRnzp   PrintResult

NegZero .FILL   -48             ; Negative ASCII "0"
NegNine .FILL   -57             ; Negative ASCII "9"

;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Write your code for hex conversion here.

ParseHex
        BRnzp   PrintResult

;;; Your code for hex conversion ends here.
;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

ParseError
        LEA     R0, ErrorStr
        PUTS                    ; Output the error string
        BRnzp   ReadString
ErrorStr
        .STRINGZ "Invalid number format.\n"

;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Write your code for decimal conversion here.

;;; Your code for decimal conversion ends here.
;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    .END

Edited by Albert_11

2
Contributors
1
Reply
12
Views
1 Week
Discussion Span
Last Post by rproffitt
0

This may be your assignment for those that want to see it well formatted:
http://www.ele.uri.edu/faculty/sendag/ele208/labs/lab06/lab6b.pdf

While I've written a lot of assembler over the years, it appears this is something other than a real microprocessor as I read https://en.wikipedia.org/wiki/LC-3

As such you might not find a lot of folk that code in this variant. Maybe that's a good thing as the more you code in Assembler the more you learn how different it can be from micro to micro.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.