![](/img/trans.png)
[英]Why these instructions which should operate on unsigned integers still affect flags used when signed numbers are dealt with?
[英]mod(x,y) works for unsigned integers, but not for signed integers in nand2tetris. What changes should I make?
@R2
M=0
@R0
D=M
@END
D, JEQ
@store
M=D
(LOOP)
@R1
D=D-M
@REMAINDER
D, JLT
@EVENLY
D, JEQ
@LOOP
0, JMP
(REMAINDER)
@R1
D=D+M
@R2
M=D
(EVENLY)
@store
D=M
@R0
M=D
(END)
@END
0, JMP
您應該確定當前代碼工作和失敗的條件。 它是否要求兩個操作數都是正數,還是只有相同的符號?
一旦你這樣做了,你也知道它失敗的條件。 然后,您可以檢查這些,並在這些條件下執行不同的代碼,或者修改操作數以使當前代碼工作並生成正確的結果。
為了幫助您入門,這里有一些模數應該返回的示例。 祝你好運!
Python 3.8.1 (default, Jan 15 2020, 18:56:16)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 3 % 2
1
>>> 3 % -2
-1
>>> -3 % -2
-1
>>> -3 % 2
1
>>> 5 % 3
2
>>> 5 % -3
-1
>>> -5 % 3
1
>>> -5 % -3
-2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.