2017-11-29 31 views
-1

숫자의 합계를 찾기 위해 어셈블러 코드를 만드는 프로젝트가 있습니다. 어셈블러 코드. 루프 및 덧셈 만 사용하여 곱셈없이 제곱합을 계산합니다. 무한 루프. ASCII/16 진수

다음 동작을 수행하기위한 어셈블리 언어 프로그램 해주기 입력은 양의 정수이고 N은 다음 N. 동일한 정수의 합을 계산을 = 1 + 4 + 9 + ⋯ +

사용자 입력이 ALWAYS 것 두 자리 숫자 (01에서 10 사이) 여야합니다. 입력의 정확성을 검사 할 필요가 없습니다. 예를 들어, 사용자 입력이 "10"이면 프로그램은 "385"를 출력합니다. 사용자 입력이

경우 03 출력되어야한다 (14), 즉 1 + 4 + 9

사용

어셈블러는 모리스 마노 책에 기초한다. 다음은 사용할 수있는 모든 코드입니다. 나는 합계를 계산하는 루핑 시스템을 생각해 냈지만 무한하고 1보다 큰 숫자를 입력하면 충돌이 발생합니다. 출력을 ASCII로 다시 변환하는 방법을 잘 모릅니다.

/read the first character 
CIF1, SKI  /skip if input flag is set 
BUN CIF1 /loop until input flag is set 
INP  /read a character from the input reg 
ADD NZR /add -30 hex (subtract 30 hex) 
STA ASD /store value into MSD 
CLA  /clear the accumulator 

/SZA /only if input is 0, otherwise it is 10 

/read the second character 
CIF2, SKI  
BUN CIF2 
INP 
ADD NZR 
STA BSD /store second number in memory 
CLA /clear accumulator 

/loop for calculation 
BUN ACCU /call subroutine ACCU to find SUM/TOTAL 

LOOP,LDA SQR /load the odd value i.e. 1, 4, 9, 16... 
ADD TWO /adds two 
ADD ODD /adds the odd number 
STA SQR /store the square 

LDA ODD /load the stored odd number 
ADD TWO /adds two current odd value 
STA ODD /stores the odd value i.e. 1, 3, 5, 7... 
/BUN ACCU /branch to subroutine to find SUM/ANSWER 


ACCU,LDA SQR //Accumulate the total SUM 
ADD SUM 
STA SUM  
LDA BSD /Load second input number 
ADD SUB /subtract 1 from input number until hits 0? 
SZA /skip out of loop 
BUN LOOP /loop to top 

/output the sum 
LDA SUM 
ADD PZR 
OUT 
HLT /end program 
/output third character????? 

/declared variables 
ODD, DEC 1 /first vale(1) and new storage for value when odd+2 
TWO, DEC 2 /add 2 to the odd numbers: 1+2=3, 3+5=7 etc. 
SQR, DEC 1 /first square(1) and stores the square i.e. 4, 9, 16, 25  
SUM, DEC 0/Starts at 1, The ANSWER or sum of numbers 
SUB, DEC -1/ Subtract 1 from input #(until 0)u 

ASD, DEC 0 /where first character is stored 
BSD, DEC 0 /where second character is stored 
CSD, DEC 0 /Third character output if needed 
NZR, HEX -30 /converts to input 
PZR, HEX 30 /converts to output 
END 
+0

당신은 디버깅을 수행해야합니다. –

+0

코드를 여러 번 추적하면서 최선을 다해 보았습니다. – snipshow7

답변

0

나는 경우 지침을 통해 단일 스테핑 (입력은 "03"입니다) 당신은 디버거에서 비슷한 일을 볼 수, 내 머리에서 디버깅하려고 할 것입니다 :

/read the first character 
CIF1, SKI  /skip if input flag is set 
BUN CIF1 /loop until input flag is set 
INP  /read a character from the input reg 
ADD NZR /add -30 hex (subtract 30 hex) 
STA ASD /store value into MSD 
    *** ASD = ACC = 0 - and it will be never touched again 
    *** (will not work for "10" input) 
CLA  /clear the accumulator 

/read the second character 
CIF2, SKI  
BUN CIF2 
INP 
ADD NZR 
STA BSD /store second number in memory 
    *** BSD = ACC = 3 
CLA /clear accumulator 

/loop for calculation 
BUN ACCU /call subroutine ACCU to find SUM/TOTAL 
    *** goes to ACCU label (it's not call, for call there is BSA instruction) 
    .... (jumping there) ... 

ACCU,LDA SQR //Accumulate the total SUM 
     *** ACC = SQR (1) 
ADD SUM 
     *** ACC += SUM (0) == 1 
STA SUM 
     *** SUM = 1 
LDA BSD /Load second input number 
     *** ACC = 3 
ADD SUB /subtract 1 from input number until hits 0? 
     *** ACC = 2 (BSD is still 3) 
SZA /skip out of loop 
BUN LOOP /loop to top 
     *** jumps to loop 

LOOP,LDA SQR /load the odd value i.e. 1, 4, 9, 16... 
     *** ACC = SQR (1) 
ADD TWO /adds two 
     *** ACC = 3 
ADD ODD /adds the odd number 
     *** ACC = 4 
STA SQR /store the square 
     *** SQR = 4 (ok) 
LDA ODD /load the stored odd number 
ADD TWO /adds two current odd value 
STA ODD /stores the odd value i.e. 1, 3, 5, 7... 
     *** ODD = 3 (you could have moved this ahead of SQR update) 
     *** (to avoid one "ADD TWO") 

ACCU,LDA SQR //Accumulate the total SUM 
     *** ACC = SQR (4) 
ADD SUM 
     *** ACC = 5 
STA SUM 
     *** SUM = 5 
LDA BSD /Load second input number 
     *** ACC = 3 
ADD SUB /subtract 1 from input number until hits 0? 
     *** ACC = 2 
SZA /skip out of loop 
BUN LOOP /loop to top 
     *** jumps to loop again (BSD is still 3) 

그리고 난을 무한 루프가 발생하는 이유는 분명합니다.

그리고 그 두 번째 LDA BSD 예상 2 ACC를로드하지 않습니다, 디버거에서 본해야하는지,하지만 3

+0

@ snipshow7도 오래된 질문 채팅을 확인하고 거기에 몇 가지 메모를 추가했지만 어떻게 알려줄지 잘 모르겠습니다. – Ped7g