2013-11-25 65 views
0

任何人都可以運行此程序,或嘗試幫助我理解爲什麼我的計數器不更新嗎?LC-3如何重置櫃檯?

我應該從提示中讀取文本,並在打印出輸入的實際文本之前查找文本的長度並用冒號輸出。

如果我第一次輸入「test」的長度是4,但是當它循環回來開始時它會要求我再次輸入,它會輸出正確的文本,但是計數器不會改變,除非文本更長。因此,如果我輸入「I」,它將輸出長度爲4,因爲測試更長,並且是4.但是如果我輸入7個字母的「Control」,它會將計數器更新爲7.

OUTPUT:

Enter: Hey 
3:Hey 
Enter: Test 
4:Test 
Enter: Control 
7:Control 
Enter: Hey 
7:Hey  <---- Length should be 3! 

謝謝!

.orig x3000    ; Starting point of the program. 

BR start   ; Branch to the start routine. 

newln: .stringz "\n" 
msg1: .stringz "Enter: " 

; Prints out the instructions to the user and reads some user input. 

start: 

lea r0, newln   ; Load address of newline into R0. 
puts     ; Print newline. 

lea r0, msg1   ; Load address of message3 into R0. 
puts 

lea r2, MESSAGE   ; Load starting point address of MESSAGE. 
and r1, r1, #0   ; Initialize R1 to zero. 

input: 
getc      ; Read in a single character to R0. 
out 
add r5, r0, #-10  ; Subtract 10 because enter key is 10. 
BRz printint    ; If zero, branch to checkChar routine. 
          ; Else continue the loop. 
str r0, r2, #0   ; Store char in MESSAGE. 
add r2, r2, #1   ; Increment index of MESSAGE. 
add r1, r1, #1   ; Increment input counter. 
BR input    ; Unconditional branch to input. 

checkChar: 
lea r5, inv81   ; Load address of inv68 into R6. 
ldr r5, r5, #0   ; Load contents of inv68 into R6 (R6 now holds  -68). 
add r0, r3, r5   ; Add -68 to the value in R3, to check if it's 'q'. 
BRz quit    ; If zero, branch to decrypt. 



; 
;print integer starts here 
; 
printint: 
ld  r3,psign 
jsr STRLEN 
    ADD  r7, r0, #0  ; get the integer to print 

    brzp nonneg 
    ld  r3,nsign 
    not  r7,r7 
    add  r7,r7,1 
nonneg: 
    lea  r6,buffer ; get the address of o/p area 
    add  r6,r6,#7 ; compute address of end of o/p 
    ld  r5,char0 ; get '0' to add to int digits 
loop1: 
    and  r0,r0,#0 ; init quotient for each divide 
loop2: 
    add  r7,r7,#-10 ; add -10 
    brn  remdr  ; until negative 
    add  r0,r0,#1 ; incr to compute quotient 
    br  loop2  ; repeat 
remdr: 
    add  r7,r7,#10 ; add 10 to get remainder 
    add  r7,r7,r5 ; convert to ascii 
    str  r7,r6,0 ; place ascii in o/p 
    add  r7,r0,#0 ; move quot for next divide 
    brz  end  ; if done then print 
    add  r6,r6,#-1 ; move to prev o/p position 
    br  loop1  ; repeat 
end: 
add  r6,r6,#-1 ; move to prev o/p position 
    str  r3,r6,0 ; place sign 
    add  r0,r6,#0 ; move address of 1st char 
    puts    ; into r0 and print 

output: 
ld r5, colon 
and r3,r3, 0; 
add r0, r3, r5; 
out 

lea r2, MESSAGE   ; Load (starting) address of MESSAGE. 

outputLoop: 
ldr r0, r2, #0   ; Load contents of address at MESSAGE index into R0. 
out      ; Print character. 
add r2, r2, #1   ; Increment MESSAGE index. 
add r1, r1, #-1   ; Decrease counter. 

BRp outputLoop   ; If positive, loop. 

br start 
quit: 

halt ; Halt execution. 

STRLEN: 
LEA R2, MESSAGE ;R1 is pointer to characters 
AND R0, R0, #0 ;R0 is counter, initially 0 
    LD R5, char0 


LOOP: ADD  R2, R2, #1 ;POINT TO NEXT CHARACTER 
LDR  R4, R2, #0 ;R4 gets character input 
BRz  FINISH 
ADD  R0, R0, #1 
BR LOOP 

FINISH: 
ADD  R0, R0, #1 
ret 

MESSAGE: .blkw   99   ; MESSAGE of size 20. 
inv48: .fill   #-48  ; Constant for converting numbers from  ASCII to decimal. 
inv81: .fill  #-81  ; Constant for the inverse of 'Q'. 
buffer: .blkw 8   ; o/p area 
null:  .fill 0   ; null to end o/p area 
char0: .fill x30 
colon  .fill x3A 
nsign  .fill x2d 
psign  .fill x20 
    .end 

回答

2

在你的例子結束,在存儲器中,起始消息的內容是: Heytrol0000000

它看起來對我來說,問題是,在STRLEN我們直到計數計算字符串的長度我們達到0的第一個字符。「Heytrol」中有7個字符。

但是,當我們存儲消息時,我們計算我們讀入的字符數(保存在r1中)。當我們稍後打印字符串時,我們使用r1中的值,所以我們不會打印任何「額外」字符。

爲了解決這個問題,我要麼輸出r1中讀取字符串時計算的值作爲其長度(完全去掉STRLEN代碼),要麼確保當我們讀取輸入循環中的輸入時,我們在去打印前寫入零字符:

input: 
getc     ; Read in a single character to R0. 
out 
add r5, r0, #-10  ; Subtract 10 because enter key is 10. 
BRz finishString  ; If zero, branch to finishString routine. 
         ; Else continue the loop. 
str r0, r2, #0   ; Store char in MESSAGE. 
add r2, r2, #1   ; Increment index of MESSAGE. 
add r1, r1, #1   ; Increment input counter. 
BR input    ; Unconditional branch to input. 

finishString: 
and r0, r0, #0   ; set r0 to zero so we can store it 
str r0, r2, #0   ; write zero (from r0) into the end of the string 
BR printint    ; Now, branch to checkChar routine.