-3

我正在解决一个在 LC-3 中制作计算器的网站上发现的问题。我正在逐步进行,并看到了一些关于使用堆栈做我目前正在做的事情。我想知道是否有人可以帮助我到目前为止将其转换为使用堆栈,到目前为止我一直在使用寄存器!我下面的代码显然是不完整的,这正是我停下来开始研究如何做一些事情时所拥有的。这是我迄今为止所拥有的 LC-3 代码:

        ;get first string from user
START   LEA R0, PROMPT1 ; Display the prompt
        PUTS
        LEA R0, BUFFER
        JSR READLINE

        ;convert string to number
        LEA R0, BUFFER
        JSR ATOI

        ;save the number
        ST  R0, FIRST_ARG

        ;get second string from user
        LEA R0, PROMPT2 ; Display the prompt
        PUTS
        LEA R0, BUFFER
        JSR READLINE

        ;convert string to number
        LEA R0, BUFFER
        JSR ATOI

        ;save the number
        ST  R0, SECOND_ARG

        ;load the arguments into R0 and R1
        LD  R0, FIRST_ARG
        LD  R1, SECOND_ARG

        JSR SUM 
PRINT_OUTPUT
        ;convert R0 into a string
        LEA R1, BUFFER
        JSR ITOA        ;not made yet, also will need division and subtraction subroutines

        ;print the sum of the two digits entered
        LEA R0, ANSWER  ; Display the prompt
        PUTS
        LEA R0, BUFFER
        PUTS

        ;print a new line character.
        LD  R0, ENTER
        OUT
STOP    HALT            ;

        ;subroutine SUM : calculates the sum of two numbers
        ;input: R0,R1
        ;output: R0 = R0 + R1
SUM
       ADD R0, R0, R1
       RET

        ;subroutine PROD : calculates the product of two numbers
        ;input: R0,R1
        ;output: R0 = R0 * R1
MUL
       ST  R2, MUL_SAVE_R2
       AND R2, R2, #0
       AND R1, R1, R1
MUL_START
       BRz MUL_END
       ADD R2, R2, R0
       ADD R1, R1, #-1
       BR  MUL_START
MUL_END
       ADD R0, R2, #0
       LD  R2, MUL_SAVE_R2
       RET
MUL_SAVE_R2 .FILL x0000

        ;subroutine READLINE : Reads a line of input from keyboard.
        ;input: R0. contains the address of the memory location where the
        ;   string must be placed.
READLINE
       ST  R7, RL_RETURN
       AND R1, R0, R0

RL_START
       ;get a character and echo it
       GETC
       OUT

       ;compare the character with ENTER which has ascii value 10
       ADD R2, R0, #-10
       BRz RL_END          ;the user typed ENTER, stop the loop
       STR R0, R1, #0      ;store whatever the user typed
       ADD R1, R1, #1      ;increment the pointer
       BR  RL_START
RL_END
       AND R0, R0, #0
       STR R0, R1, #0      ;write the null character
       LD  R7, RL_RETURN
       RET
RL_RETURN  .FILL x0000

;subroutine ATOI : Converts an ASCII string to an integer
;input: R0, contains the address of the string
;output: R0, should contain the integer value
ATOI
       ST  R7, ATOI_RETURN

       AND R2, R0, R0      ;R2 <- R0;
       AND R0, R0, #0      ;R0 <- 0
       ADD R1, R0, #10     ;R1 <- 10

       LD  R4, ASCIIZERO
       NOT R4, R4
       ADD R4, R4, #1      ;This is to convert ascii character to integer

ATOI_START
       LDR R5, R2, #0
       BRz ATOI_END        ;we've reached the end of the string

       AND R1, R1, #0
       ADD R1, R1, #10     ;R1 <- 10
       JSR MUL            ;multiply current number by 10

       ADD R5, R5, R4      ;subtract ASCIIZERO from R5
       BRn INVALID_INPUT   ;user typed something less than '0'
       ADD R6, R5, #-9
       BRp INVALID_INPUT   ;user typed something more than '9'

       ADD R0, R0, R5
       ADD R2, R2, #1      ;next character
       BR  ATOI_START
INVALID_INPUT
       AND R0, R0, #0      ;make R0 <- -1
       ADD R0, R0, #-1
ATOI_END
       LD  R7, ATOI_RETURN
       RET

ATOI_RETURN .FILL x0000


;allocate memory for the input
FIRST_ARG  .FILL x0000
SECOND_ARG .FILL x0000
BUFFER     .BLKW #15  ; allocating memory for storing user input.

POP
        LDR R0, R6, #0
        ADD R6, R6, #1
        RET

PUSH
        ADD R6, R6, #-1
        STR R0, R6, #0
        RET

;constants
MINUS      .FILL x002D; '-'
ENTER      .FILL x000A; newline character
PROMPT1    .STRINGZ "Please input the first digit > "
PROMPT2    .STRINGZ "Please input the second digit > "
ANSWER     .STRINGZ "Sum of the two digits entered = "
ASCIIZERO  .FILL x0030; '0'
.END
4

2 回答 2

0

制作计算器的一种直接方法是制作一个使用逆波兰表示法(RPN) 的计算器。

当您使用 RPN 时,您逐个读取输入令牌并在每次读取后执行一个操作。如果令牌是数字,则将其压入堆栈。如果是运算符,则将元素从堆栈中弹出并处理它们。例如,如果运算符是+,则弹出堆栈顶部的两个元素并将它们相加,将总和压入堆栈。当你读完标记后,栈顶元素就是你的答案。

为了更容易获得令牌,可能需要将令牌用一个空格分隔(至少开始时)。

因此,例如,用户可能会输入:

7 5 + 12 /

你会:

  1. 读取令牌7push它(堆栈现在7
  2. 读取令牌5push它(堆栈现在是5, 7
  3. 读取令牌+,弹出75放入寄存器,添加它们,push结果(现在堆栈12
  4. 读取令牌12push它(堆栈现在是12, 12
  5. 读取 token /pop 12并将12其写入寄存器,将它们分开,push结果(现在是堆栈1)。
  6. Pop将最终答案存入寄存器并打印结果 ( 1)。

Unix 实用程序dc就是这样工作的(或者至少像这样工作)。

使用其他类型的符号,包括标准的“中缀”符号,将更加困难。我认为最优秀的 C++ 编程语言有(或者如果现在没有的话,在早期版本中就有)一个中缀计算器的玩具示例,让您了解所涉及的内容。

于 2014-12-10T08:13:48.420 回答
0

如果您要询问将变量传递给堆栈上的函数,那么一方面,您不需要这样做。使用寄存器是优化器所渴望的。

但是,机制是将它们压入堆栈(R13),调用函数,然后添加返回,将变量的大小添加到堆栈指针(2 x 32 位数字 = 8 字节)。

我相信在 ARM 中,帧指针(R11)会在调用函数之前自动指向堆栈。因此,如果将 2 x 32 位数字压入堆栈,则第一个将在 [R11, #-8] 处,第二个将在 [R11, #-4] 处(记住堆栈从末尾开始增长)。

于 2014-12-10T09:03:39.117 回答