在汇编器中移动(移位)两个寄存器

发布于 2024-12-25 08:17:58 字数 1868 浏览 1 评论 0原文

我有 8 个 LED 和 2 个按钮,最初第一个和最后一个 LED 亮起,如果我按第一个按钮,第一个 LED 向右移动,如果我按第二个按钮,最后一个 LED 向左移动。如果两个灯“相遇”,通过相互移动,它们就会关闭。

我不知道按下按钮后如何更新单行上的 LED,因此我为每个按钮制作了 2 行 P1 和 P2。第一行看起来转移得很好,但第二行确实一团糟。我做错了什么?

下面是我的代码:

$TITLE(5)
$MOD51
ORG 100H
START: 
    ; 8 leds, P1.0-P1.7
    ; 2 buttons, P0.1 P0.2
    MOV P1, #00h
    MOV P2, #00h

    ; left LED positions
    MOV 30H, #00000001b ; initial position
    MOV 31H, #00000010b  
    MOV 32H, #00000100b  
    MOV 33H, #00001000b  
    MOV 34H, #00010000b             
    MOV 35H, #00100000b  
    MOV 36H, #01000000b  
    MOV 37H, #10000000b 

    MOV 38H, #00000000b ; leds meet

    ; right led positions
    MOV 39H, #10000000b ; initial position
    MOV 40H, #01000000b  
    MOV 41H, #00100000b  
    MOV 42H, #00010000b  
    MOV 43H, #00001000b             
    MOV 44H, #00000100b  
    MOV 45H, #00000010b  
    MOV 46H, #00000001b  

    MOV R1, #0
    MOV R2, #0

 LOOP:  
    JNB P0.1, INCREMENT_L ; left button pressed, led shifts right
    JNB P0.2, INCREMENT_R ; right button pressed, led shifts left

    CALL DISP_L ; display left led
    CALL DISP_R ; display right led

    JMP LOOP

INCREMENT_L:
    SETB P0.1
    CJNE R1, #7, INC_L
    MOV R1, #0
    JMP LOOP

INCREMENT_R:
    SETB P0.2
    CJNE R2, #7, INC_R
    MOV R2, #0
    JMP LOOP

INC_L:
    MOV A, R2
    ADD A, #39H
    MOV B, A
    MOV A, R1
    ADD A, #30H
    CJNE A, B, INCL
    JMP RESET
    JMP LOOP

INC_R:
    MOV A, R1
    ADD A, #30H
    MOV B, A
    MOV A, R2
    ADD A, #39H
    CJNE A, B, INCR
    JMP RESET
    JMP LOOP

INCL:
    INC R1
    JMP LOOP

INCR:
    INC R2
    JMP LOOP

DISP_L:
    MOV A, R1
    ADD A, #30H
    MOV R0, A
    MOV P1, @R0
    RET

DISP_R: 
    MOV A, R2
    ADD A, #39H
    MOV R0, A
    MOV P2, @R0
    RET

RESET:
    MOV R1, #0
    MOV R2, #0
    JMP LOOP

END

I have 8 LEDs and 2 buttons, initially the first and the last LED are lit, if i press the first button, the first led shifts right, if i press the second button the last led shifts left. If the two lights "meet", by shifting upon each other, they switch off.

I did not know how to update the leds on a single row after pressing the buttons, so I made 2 rows, P1 and P2, for each button. The first row seems to shift properly, but the second is really messed up. What am I doing wrong?

Below is my code:

$TITLE(5)
$MOD51
ORG 100H
START: 
    ; 8 leds, P1.0-P1.7
    ; 2 buttons, P0.1 P0.2
    MOV P1, #00h
    MOV P2, #00h

    ; left LED positions
    MOV 30H, #00000001b ; initial position
    MOV 31H, #00000010b  
    MOV 32H, #00000100b  
    MOV 33H, #00001000b  
    MOV 34H, #00010000b             
    MOV 35H, #00100000b  
    MOV 36H, #01000000b  
    MOV 37H, #10000000b 

    MOV 38H, #00000000b ; leds meet

    ; right led positions
    MOV 39H, #10000000b ; initial position
    MOV 40H, #01000000b  
    MOV 41H, #00100000b  
    MOV 42H, #00010000b  
    MOV 43H, #00001000b             
    MOV 44H, #00000100b  
    MOV 45H, #00000010b  
    MOV 46H, #00000001b  

    MOV R1, #0
    MOV R2, #0

 LOOP:  
    JNB P0.1, INCREMENT_L ; left button pressed, led shifts right
    JNB P0.2, INCREMENT_R ; right button pressed, led shifts left

    CALL DISP_L ; display left led
    CALL DISP_R ; display right led

    JMP LOOP

INCREMENT_L:
    SETB P0.1
    CJNE R1, #7, INC_L
    MOV R1, #0
    JMP LOOP

INCREMENT_R:
    SETB P0.2
    CJNE R2, #7, INC_R
    MOV R2, #0
    JMP LOOP

INC_L:
    MOV A, R2
    ADD A, #39H
    MOV B, A
    MOV A, R1
    ADD A, #30H
    CJNE A, B, INCL
    JMP RESET
    JMP LOOP

INC_R:
    MOV A, R1
    ADD A, #30H
    MOV B, A
    MOV A, R2
    ADD A, #39H
    CJNE A, B, INCR
    JMP RESET
    JMP LOOP

INCL:
    INC R1
    JMP LOOP

INCR:
    INC R2
    JMP LOOP

DISP_L:
    MOV A, R1
    ADD A, #30H
    MOV R0, A
    MOV P1, @R0
    RET

DISP_R: 
    MOV A, R2
    ADD A, #39H
    MOV R0, A
    MOV P2, @R0
    RET

RESET:
    MOV R1, #0
    MOV R2, #0
    JMP LOOP

END

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别挽留 2025-01-01 08:17:58

这听起来很像一个签名/未签名的问题。您的添加是无符号的,还是假设高位是符号位并为您执行 2 补码操作。如果不小心的话,任何剩下的进位也可能会影响事情。你有直接移位指令而不是使用加法吗?

很快在网上查了一下,发现你确实有一条 SHL/SHR 指令,可以用来移动你的位。然后,您只需对您的位置进行异或即可在匹配时关闭该位。

This sounds an awful lot like a signed/unsigned issue. Is your add unsigned, or does it assume the high bit is a sign bit and do the 2s compliment stuff for you. Any carry bits left over could also affect things if not careful as well. Do you have a straight shift instruction instead of using add?

Looked online real quick and see you do have a SHL/SHR instruction you could use to move your bits around. Then, you just xor against your location to turn the bit off if they match.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文