2017-11-29 6 views
-1

数字の合計を見つけるためのアセンブラコードを作成するプロジェクトがあります。アセンブラコード。ループと加算のみを使用して乗算なしの平方和を計算する。無限ループ。 ASCII/16進数

は、以下の操作を実行するためのアセンブリ言語プログラムを書く:入力の正の整数Nは、常にユーザー入力N.
= 1 + 4 + 9 +⋯+
に等しい整数の和であろう計算2桁の数字(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) 

そしてIなぜ無限ループが起こるのかは明らかです。

そして、それはあなたが二LDA BSDが期待される2とACCをロードしないことを、デバッガで見ているべきものだが、3

+0

@ snipshow7も古い質問のチャットをチェックして、そこにいくつかのメモを追加したが、あなたに通知する方法がわからない。 – Ped7g

関連する問題