简体   繁体   中英

Storing in LC-3 issues

So I'm working on an assignment in LC-3 assembly language, and I'm having an issue. So the program is designed to use subroutine GETDEC to take in some input as a string from the keyboard and then interprets the input string to construct the integer value. I think I'm close to getting the assignment done, but I seem to be stuck. We are meant to use a block of 8 words for the input buffer, and my issue is, that I can't seem to get my program to save the input string values in the block, which in turn prevents me from being able get out of the loop in my main method.

Here is the main method

    LEA     R1, DATA
INPUT
LD  R0, NEWLN
TRAP    x21
LEA R0, PROMPT
TRAP    x22

JSR GETDEC
STR R0, R1, #0
ADD R1, R1, #1
ADD R0, R0, #0
BRNP    INPUT

LD  R0, NEWLN
TRAP    x21
LEA R1, DATA

OUTPUT

LDR R0, R1, #0
JSR PUTDEC
LD  R0, NEWLN
TRAP    x21
ADD R1, R1, #1
LDR R0, R1, #-1
BRNP    OUTPUT

TRAP    x25
  NEWLN .FILL   x000A
  PROMPT    .STRINGZ "Enter an integer, 0 to quit> "
  DATA  .BLKW   10

Here is my GETSTR subroutine, which gets the input as a string value

    GETSTR

ST  R7, GET_7
ST  R1, S1
ST  R2, S2
ST  R3, S3


ADD R1, R0, 0
TRAP    x20     ;User input
TRAP    x21     ;echoes input
STR R0,R1,0
AND R3,R3,0     ;clears R3
ADD R2, R0, 0   ;R2 <- R0
ADD R1, R1, #1  ;R3 <- R3+1 
LD  R3, PRZ     ;R1 <- xF6 (-10)
ADD R2,R3,R2    ;R2 <- R1 + R2
BRnp    -9      ;Branches back to User Input unless last flag is Zero

LD  R1, S1
LD  R2, S2
LD  R3, S3
LD  R7, GET_7

    RET

 GET_7  .BLKW   1
 PRZ    .FILL   xFFF6
 S1         .BLKW   1
 S2         .BLKW   1
 S3         .BLKW   1

And here's my GETDEC subroutine that takes the input string and interprets it into the corresponding integer value.

    GETDEC  ;Input a signed integer to R0

ST  R7, GET7
ST  R1, GET1
ST  R2, GET2

LEA R0, JIMB    ;Input a character string
JSR GETSTR
LD  R2, JIMB
AND R0, R0, 0

    HERE    ADD R0, R0, R0  ; multiply by 10
ADD R1, R0, R0
ADD R1, R1, R1
ADD R0, R0, R1
ADD R0, R0, R2  ; R0 <- R0 + X
ADD R2, R2, 1   ; Increments R2
LD  R3, WORK
ADD R3, R2, R3
BRnp    HERE        ;Loop if R2 = 0

LD  R1, GET1
LD  R2, GET2
LD  R7, GET7
RET

    GET1    .BLKW   1
    GET7    .BLKW   1
    GET2    .BLKW   1
    JIMB    .BLKW   8
    WORK    .FILL   xFFD0

So to reiterate, the main method loops GETDEC until a 0 is entered at input, but something in my code is preventing me from breaking out of the loop when 0 is entered. I think I might be using the wrong Load methods within the GETDEC subroutine, but I'm not sure, if anyone can help, it'd be greatly appreciated.

I thing the storing is OK, and your problem is here (in GETDEC):

JSR GETSTR
LD  R2, JIMB <=

You load the data from the buffer here, not the buffer address. You probably need LEA.

Then here:

ADD R0, R0, R2  ; R0 <- R0 + X
ADD R2, R2, 1   ; Increments R2

The first line treats R2 like data, and second line as pointer to the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM