2015-04-04 60 views
-1

我爲LC-3編寫了一個程序,用於輸入10以下的數字,直到達到0,然後輸出輸入的最大數字。LC-3程序正在返回一些無法打印的東西

一切似乎工作,但我不斷得到一個不正確的,或不存在的結果。例如,如果我輸入1,2,3,然後0,它應該說:

零輸入,結束程序。輸入的最大整數是3

但我沒有得到任何回報。

我試圖用來輸出最大的整數的方法是使用二進制補碼系統來確定更大的二進制數,如果沒有進入0則返回,但我認爲我的邏輯有一個主要問題。

對不起,格式化可能有些不可靠,如果您需要更多信息或者我做了錯誤的事情(關於後 - 我知道我的代碼是錯誤的),請告訴我。

預先感謝您! (我也有在代碼的底部的樣本輸出。)

.ORIG x3000 

    AND  R0,R0,#0  ;clear R0 
    AND  R0,R0,#0  ;clear R1 
    LEA   R0, MSG1  ;load address of message 1 
    PUTS     ;display message 

    GETC     ;read in character from keyboard 
    OUT     ;echo input 
    ST  R0, NUM1  ;store the number in num1 
    LD  R2, NUM1 
    LD  R1, POS48 
    ADD  R2,R2, R1  ;adds 48 to make the character a number 
    BRz  ZERO   ;checks if the number is zero 

    LD  R0, NEWLINE  ;load newline 
    OUT     ;execute newline 

    LOOP LEA   R0, MSG1  ;load address of message 1 
    PUTS     ;display message 

    GETC     ;read in character from keyboard 
    OUT     ;echo input 
    ST  R0, NUM2  ;store character in num2 
    LD  R2, POS48 
    LD  R3, NUM2 
    ADD  R0, R3, R2  ;adds 48 to make the character a number 
    BRz  ZERO   ;checks if the number is zero 

    LD  R0, NEWLINE  ;load newline 
    OUT     ;execute newline 

    LD  R1, NUM1  ;load the first number 
    LD  R2, NUM2  ;load the second number 
    NOT  R2, R2   ;two's complement of R2 
    ADD  R2, R2, #1  ;getting negative of num 2 
    ADD  R0, R1, R2  ;adding the two values 
    ST  R0, MAX   ;storing larger number in NUM5 


    BRnz  LOOP   ;Branch if R0 is positive 

    ZERO LEA  R0, MSG2  ;load message if number entred is zero 
    PUTS     ;display message 
    LD  R0, NEWLINE  ;load newline 
    OUT     ;execute newline 
    LEA  R0, MSG3  ;load largest int message 
    PUTS     ;display 

    LD  R2, MAX 
    LD  R1, POS48 
    ADD  R1, R2, R1  ;adds 48 to make the character a number 
    LD  R0, MAX   ;load largest int 
    OUT     ;display largest int 
    HALT     ;end program 

    ;*** Data *** 
    MSG1  .STRINGZ "Enter a single-digit integer: " 
    MSG2  .STRINGZ "Zero entered, ending program." 
    MSG3  .STRINGZ "The largest integer is: " 
    POS48  .FILL  #48 
    NEWLINE  .FILL  #10 
    NUM1  .BLKW  1 
    NUM2  .BLKW  1 
    MAX   .BLKW  1 

    .END 

樣本輸出 - 輸入一個單位的整數:1 輸入一個單個位的整數:2 輸入一個單個位的整數:3 輸入一位數的整數:0 零輸入,結束程序。

回答

0

調試完程序後,我能夠在代碼中找到兩個邏輯錯誤。首先是當你試圖在整數值和ascii值之間進行轉換時。第二個是當你比較兩個數字來找出更大的數字時。我已經重新創建了程序的工作版本,並對其進行了非常徹底的評論,因此應該很容易遵循。


第一個問題是你的ascii整數轉換代碼。你試圖用一個等於48的POS48從ASCII碼轉換成一個整數值;但是,爲了從ascii轉換爲整數,您必須刪除48,或者在彙編中添加-48。然後從一個整數值的ASCII值轉換回你會增加正48

例如:

NEG48 .FILL xFFD0 ;create constant NEG48 which = xFFD0 = -48 
LD  R6, NEG48   ;load NEG48 into register 6 
GETC      ;get user input 
ADD  R0, R0, R6  ;add -48 to the user input to get integer value 

第二個問題是與比較兩個整數值發現這是代碼大。在你的代碼中,你得到了兩個人的讚美,獲得了第二個值的負值,然後將它添加到第一個值。除了您需要使用BRn和BRp來評估結果是正面還是負面之外,這是正確的。如果該值爲正值,則表示第一個數字較大,如果爲負值,則第二個數字較大。 BRn將分支爲負值,並且如果正值則BRp將分支。

對於實施例下面的代碼將比較由用戶在程序中輸入的前面由用戶輸入的電流最大值的新號碼,然後保存該較大值到CURRENTMAX變量:

LD R2, CURRENTMAX ;load CURRENTMAX 
LD R3, NEWNUM  ;load NEWNUM 
NOT R4, R3   ;two's complement of NEWNUM 
ADD R4, R4, #1  ;getting negative of NEWNUM 
ADD R1, R4, R2  ;adding the negative of NEWNUM to CURRENTMAX 
BRn LrgR3   ;if the result in R1 is negative NEWNUM is larger so branch to LrgR3 
BRp LrgR2   ;if the result in R1 is positive CURRENTMAX is larger so branch to LrgR2 

;**************** LrgR3 **************** 
LrgR3    ;begin LrgR3 
ST R3, CURRENTMAX ;store the larger result in CURRENTMAX 
BR LOOP   ;branch to LOOP 

;**************** LrgR2 **************** 
LrgR2    ;begin LrgR2 
ST R2, CURRENTMAX ;store the larger result in CURRENTMAX 
BR LOOP   ;branch to LOOP 

最後,這裏是我爲了幫助你更好地解決這個問題而編寫的重寫版本。

.ORIG x3000 

;**************** POLL INITIAL USER INPUT **************** 
LD R5, POS48  ;load num to char conversion value 
LD R6, NEG48  ;load char to num conversion value 

LEA R0, MSG1  ;load MSG1 
PUTS    ;display MSG1 
GETC    ;get user input 
OUT     ;display users input 

ADD R0, R0, R6  ;convert input char into a number value 
ST R0, CURRENTMAX ;store numer value into MAX since the only number entered is obviously the MAX value entered 
BRz ZERO   ;if zero branch to ZERO 

LD R0, NEWLINE  ;load newline 
OUT     ;display newline 

;**************** LOOP **************** 
LOOP    ;begin LOOP 
LEA R0, MSG1  ;load MSG1 
PUTS    ;display MSG1 
GETC    ;get under input 
OUT     ;display user input 

ADD R0, R0, R6  ;convert user input char into a number value 
ST R0, NEWNUM  ;store number value into NEWNUM 
BRz ZERO   ;if zero branch to ZERO 

LD R0, NEWLINE  ;load newline 
OUT     ;display newline 

LD R2, CURRENTMAX ;load CURRENTMAX 
LD R3, NEWNUM  ;load NEWNUM 
NOT R4, R3   ;two's complement of NEWNUM 
ADD R4, R4, #1  ;getting negative of NEWNUM 
ADD R1, R4, R2  ;adding the negative of NEWNUM to CURRENTMAX 
BRn LrgR3   ;if the result in R1 is negative NEWNUM is larger so branch to LrgR3 
BRp LrgR2   ;if the result in R1 is positive CURRENTMAX is larger so branch to LrgR2 

;**************** LrgR3 **************** 
LrgR3    ;begin LrgR3 
ST R3, CURRENTMAX ;store the larger result in CURRENTMAX 
BR LOOP   ;branch to LOOP 

;**************** LrgR2 **************** 
LrgR2    ;begin LrgR2 
ST R2, CURRENTMAX ;store the larger result in CURRENTMAX 
BR LOOP   ;branch to LOOP 

;**************** ZERO **************** 
ZERO    ;begin ZERO 
LD R0, NEWLINE  ;load newline 
OUT     ;display newline 
LEA R0, MSG2  ;load MSG2 
PUTS    ;display MSG2 
LD R0, NEWLINE  ;load newline 
OUT     ;display newline 
LEA R0, MSG3  ;load MSG3 
PUTS    ;display MSG3 

LD R0, CURRENTMAX ;load CURRENTMAXvalue 
ADD R0, R0, R5  ;convert CURRENTMAX num into char 
OUT     ;display CURRENTMAX's char value 
HALT    ;end program 


;**************** Variables and Constants **************** 
MSG1  .STRINGZ "Enter a single-digit integer: " 
MSG2  .STRINGZ "Zero entered, ending program." 
MSG3  .STRINGZ "The largest integer is: " 
POS48  .FILL  x30 
NEG48  .FILL  xFFD0 
NEWLINE  .FILL  #10 
NEWNUM  .BLKW  1 
CURRENTMAX .BLKW  1 

.END