라벨을 사용하여 Little Man 컴퓨터 프로그램 BubbleSort를 단순화하고 싶습니다.
이 코드는 입력, 출력을 Bubblesorts하고 사용하고, 자체적으로 정렬 한 다음 반복하는 Little Man Computer 프로그램입니다. 이것은 프로그램이지만 코드를 단순화하고 더 읽기 쉽게 만들기 위해 분기가 이동할 모든 상수, 변수 및 분기 대상 위치에 대해 레이블을 사용하고 싶습니다. 유지 관리를 개선하기 위해 어떤 레이블 이름을 사용해야할지 모르겠습니다. 숫자 코드는 필요하지 않습니다. 줄 번호, 레이블, 니모닉 데이터 및 설명 만.
000 IN 9001 // input count
001 STO 090 3090 // store count
002 LDA 096 5096 // STO
003 ADD 095 1095 // Determine first location
004 STO 011 3011 // Overwrite STO instruction for list
005 ADD 090 1090
006 STO 092 3092 // Store STO + LOC + Count to determine end
007 LDA 011 5013 // Load manipulated instruction (using as counter)
008 SUB 092 2092 //
009 BRZ 016 7016 // If last count, go to END INPUT LIST
010 IN 9001
011 DAT 0 // manipulated instruction (store input in list)
012 LDA 011 5011
013 ADD 098 1098 // increment store instruction (to next list location)
014 STO 011 3011 // Update STO instruction
015 BR 007 6007 // GOTO INPUT LIST LOOP
016 LDA 098 5098
017 SUB 090 2090 // 1 – count
018 BRP 061 8061 // GO TO END I LOOP
019 LDA 099 5099
020 STO 092 3092 // set I to zero (0)
021 LDA 090 5090
022 SUB 098 2098 // COUNT - 1
023 SUB 092 1092 // COUNT -1 – I
024 BRZ 061 7061 // if(I == count - 1) GOTO END I LOOP
025 LDA 090 5090
026 SUB 098 2098
027 STO 093 3093 // J = Count – 1
028 LDA 092 5092 // I
029 SUB 093 2093 // I - J
030 BRP 057 8057 // If I == j, then GO END J LOOP
031 LDA 097 5097 // load LDA instruction numeric code
032 ADD 095 1095 // set to LDA 500
033 ADD 093 1093 // set to LDA [500 + j] or A[j]
034 STO 039 3039 // reset instruction
035 SUB 098 2098 // set to LDA [500 + j – 1] or A[j-1]
036 STO 037 3037 // reset instruction
037 DAT 0 // load A[j-1] (instruction is manipulated)
038 STO 088 3088
039 DAT 0 // load A[j] (instruction is manipulated)
040 STO 089 3089
041 SUB 088 2088 // A[j] – A[j-1] (swap if not positive)
042 BRP 053 8053 // GOTO DECREMENT J
043 LDA 096 5096 // load STO instruction code
044 ADD 095 1095 // set to STO 500
045 ADD 093 1093 // set to STO [500 + j]
046 STO 052 3052 // reset instruction
047 SUB 098 2098 // set to STO [500 + j – 1]
048 STO 050 3050 // reset instruction
049 LDA 089 5089 // load A[j]
050 DAT 0 // Store in A[j-1] (instruction is manipulated)
051 LDA 088 5088 // load A[j-1]
052 DAT 0 // Store in A[j] (instruction is manipulated)
053 LDA 093 5093
054 SUB 098 2098
055 STO 093 3093 // J = J – 1
056 BR 028 6028 // GOTO START J LOOP
057 LDA 092 5092
058 ADD 098 1098
059 STO 092 3092 // I = I + 1
060 BR 021 6021 // GOTO START I LOOP
061 LDA 090 5090 // Count
062 OUT 9002
063 LDA 097 5097
064 ADD 095 1095 // LDA + LOC
065 STO 071 3071 // set up instruction
066 ADD 090 1090 // LDA + LOC + Count
067 STO 092 3092 // store unreachable instruction
068 LDA 071 5071 // load manipulated instruction (used as counter)
069 SUB 092 2092
070 BRZ 077 7077 // GOTO END OUTPUT LOOP
071 DAT 0 // manipulated output
072 OUT 9002
073 LDA 071 5071
074 ADD 098 1098
075 STO 071 3071 // increment manipulated instruction
076 BR 068 6028 // GOTO OUTPUT LIST LOOP
077 BR 0 6000 // Branch to top of loop (embedded)
078 HLT 0 // (Should never hit this instruction)
088 DAT 0 // A[j-1] value (also used for swapping)
089 DAT 0 // A[j] value (also used for swapping)
090 DAT 0 // count variable (input and output)
091 DAT 0 // unused
092 DAT 0 // ‘I’ counter
093 DAT 0 // ‘j’ counter
094 DAT 0 // unused
095 DAT 500 // initial list location
096 DAT 3000 // STO instruction
097 DAT 5000 // LDA instruction
098 DAT 1 // one (constant)
099 DAT 0 // zero (constant)
답변
주석을 레이블에 대한 영감으로 사용하십시오. 작업의 대상이 아닌 라인은 레이블없이 이동할 수 있습니다. 예를 들어 :
start IN // input count
STO count // store count
LDA stoInstruction // STO
ADD location // Determine first location
STO storeInput // Overwrite STO instruction for list
ADD count
STO i // Store STO + LOC + Count to determine end
loopInput LDA storeInput // Load manipulated instruction (using as counter)
SUB i //
BRZ exitInputLoop // If last count, go to END INPUT LIST
IN
storeInput DAT // manipulated instruction (store input in list)
LDA storeInput
ADD one // increment store instruction (to next list location)
STO storeInput // Update STO instruction
BR loopInput // GOTO INPUT LIST LOOP
exitInputLoop LDA one
SUB count // 1 – count
BRP exitLoopI // GO TO END I LOOP
LDA zero
STO i // set I to zero (0)
loopI LDA count
SUB one // COUNT - 1
SUB i // COUNT -1 – I
BRZ exitLoopI // if(I == count - 1) GOTO END I LOOP
LDA count
SUB one
STO j // J = Count – 1
loopJ LDA i // I
SUB j // I - J
BRP exitLoopJ // If I == j, then GO END J LOOP
LDA ldaInstruction // load LDA instruction numeric code
ADD location // set to LDA 500
ADD j // set to LDA [500 + j] or A[j]
STO loadCurrent // reset instruction
SUB one // set to LDA [500 + j – 1] or A[j-1]
STO loadPrevious // reset instruction
loadPrevious DAT // load A[j-1] (instruction is manipulated)
STO previous
loadCurrent DAT // load A[j] (instruction is manipulated)
STO current
SUB previous // A[j] – A[j-1] (swap if not positive)
BRP decrementJ // GOTO DECREMENT J
LDA stoInstruction // load STO instruction code
ADD location // set to STO 500
ADD j // set to STO [500 + j]
STO storeCurrent // reset instruction
SUB one // set to STO [500 + j – 1]
STO storePrevious // reset instruction
LDA current // load A[j]
storePrevious DAT // Store in A[j-1] (instruction is manipulated)
LDA previous // load A[j-1]
storeCurrent DAT // Store in A[j] (instruction is manipulated)
decrementJ LDA j
SUB one
STO j // J = J – 1
BR loopJ // GOTO START J LOOP
exitLoopJ LDA i
ADD one
STO i // I = I + 1
BR loopI // GOTO START I LOOP
exitLoopI LDA count // Count
OUT
LDA ldaInstruction
ADD location // LDA + LOC
STO instruction // set up instruction
ADD count // LDA + LOC + Count
STO i // store unreachable instruction
loopOutput LDA instruction // load manipulated instruction (used as counter)
SUB i
BRZ exitLoopOutput // GOTO END OUTPUT LOOP
instruction DAT // manipulated output
OUT
LDA instruction
ADD one
STO instruction // increment manipulated instruction
BR loopOutput // GOTO OUTPUT LIST LOOP
exitLoopOutput BR start // Branch to top of loop (embedded)
HLT // (Should never hit this instruction)
previous DAT // A[j-1] value (also used for swapping)
current DAT // A[j] value (also used for swapping)
count DAT // count variable (input and output)
DAT // unused
i DAT // ‘I’ counter
j DAT // ‘j’ counter
DAT // unused
location DAT 500 // initial list location
stoInstruction DAT 3000 // STO instruction
ldaInstruction DAT 5000 // LDA instruction
one DAT 1 // one (constant)
zero DAT 0 // zero (constant)
메모:
이 LMC는 원래 LMC의 변형으로 3 자리 숫자를 사용했지만 4 자리 숫자를 사용하는 것으로 작업하는 것 같습니다.
코드는 그다지 간결하지 않습니다. 입력 데이터에 필요한 저장소를 제외하고 98 개의 사서함을 사용합니다. 적은 수로 할 수 있습니다. 75 개의 사서함을 사용하는 이 구현 을 살펴보십시오 .
라인 번호가 필요하다고 작성하지만 레이블을 사용할 때 라인 번호 (즉, 우편함 번호)는 무관 해집니다. LMC- 어셈블러는 조립 중에 할당 할 수 있습니다.
표준 100 개 사서함 LMC에서 실행
귀하의 의견 후에 표준 LMC에 맞게 조정 된 코드 버전을 여기에 제공합니다. 이는 실제 입력 데이터를위한 공간이 많지 않다는 것을 의미합니다. 데이터를 위해 11 개의 사서함 만 남았습니다.
다음 부분을 교체해야했습니다.
location DAT 500 // initial list location
stoInstruction DAT 3000 // STO instruction
ldaInstruction DAT 5000 // LDA instruction
... 이와 함께 :
location DAT list // initial list location
stoInstruction DAT 300 // STO instruction
ldaInstruction DAT 500 // LDA instruction
list DAT // start of the list
이것은 표준 LMC에서와 같이 필요합니다.
- opcode는 4 자리가 아니라 3 자리입니다.
- 사서함 주소는 3이 아니라 2 자리입니다 (따라서 500은 허용되지 않습니다).
- 하드 코딩 된 사서함 주소 대신 목록 위치로 사용 가능한 다음 주소를 사용하는 것이 좋습니다.
또한 사용하지 않는 사서함을 정의하는 두 줄을 제거했습니다.
마지막으로 이전 명령 이 부정적인 결과를 냈을 때 누산기의 값이 무엇인지 이론적으로 보장 할 수 없기 때문에 BRZ
명령으로 두 명령을 변경합니다 . 이 경우 누산기의 값은 신뢰할 수 없습니다 (음수가 아닌 값만 가질 수 있으므로 Wikipedia 참조). 따라서 정의되지 않은 값에 대해 수행하는 것은 위험을 감수합니다. 누산기가 아닌 플래그를 확인하므로 안전한 명령어입니다.BRP
SUB
BRZ
BRP
#input: 3 44 22 99
start IN // input count
STO count // store count
LDA stoInstruction // STO
ADD location // Determine first location
STO storeInput // Overwrite STO instruction for list
ADD count
STO i // Store STO + LOC + Count to determine end
loopInput LDA storeInput // Load manipulated instruction (using as counter)
SUB i //
BRP exitInputLoop // If last count, go to END INPUT LIST
IN
storeInput DAT // manipulated instruction (store input in list)
LDA storeInput
ADD one // increment store instruction (to next list location)
STO storeInput // Update STO instruction
BR loopInput // GOTO INPUT LIST LOOP
exitInputLoop LDA one
SUB count // 1 – count
BRP exitLoopI // GO TO END I LOOP
LDA zero
STO i // set I to zero (0)
loopI LDA count
SUB one // COUNT - 1
SUB i // COUNT -1 – I
BRZ exitLoopI // if(I == count - 1) GOTO END I LOOP
LDA count
SUB one
STO j // J = Count – 1
loopJ LDA i // I
SUB j // I - J
BRP exitLoopJ // If I == j, then GO END J LOOP
LDA ldaInstruction // load LDA instruction numeric code
ADD location // set to LDA 500
ADD j // set to LDA [500 + j] or A[j]
STO loadCurrent // reset instruction
SUB one // set to LDA [500 + j – 1] or A[j-1]
STO loadPrevious // reset instruction
loadPrevious DAT // load A[j-1] (instruction is manipulated)
STO previous
loadCurrent DAT // load A[j] (instruction is manipulated)
STO current
SUB previous // A[j] – A[j-1] (swap if not positive)
BRP decrementJ // GOTO DECREMENT J
LDA stoInstruction // load STO instruction code
ADD location // set to STO 500
ADD j // set to STO [500 + j]
STO storeCurrent // reset instruction
SUB one // set to STO [500 + j – 1]
STO storePrevious // reset instruction
LDA current // load A[j]
storePrevious DAT // Store in A[j-1] (instruction is manipulated)
LDA previous // load A[j-1]
storeCurrent DAT // Store in A[j] (instruction is manipulated)
decrementJ LDA j
SUB one
STO j // J = J – 1
BR loopJ // GOTO START J LOOP
exitLoopJ LDA i
ADD one
STO i // I = I + 1
BR loopI // GOTO START I LOOP
exitLoopI LDA count // Count
OUT
LDA ldaInstruction
ADD location // LDA + LOC
STO instruction // set up instruction
ADD count // LDA + LOC + Count
STO i // store unreachable instruction
loopOutput LDA instruction // load manipulated instruction (used as counter)
SUB i
BRP exitLoopOutput // GOTO END OUTPUT LOOP
instruction DAT // manipulated output
OUT
LDA instruction
ADD one
STO instruction // increment manipulated instruction
BR loopOutput // GOTO OUTPUT LIST LOOP
exitLoopOutput BR start // Branch to top of loop (embedded)
HLT // (Should never hit this instruction)
previous DAT // A[j-1] value (also used for swapping)
current DAT // A[j] value (also used for swapping)
count DAT // count variable (input and output)
i DAT // ‘I’ counter
j DAT // ‘j’ counter
location DAT list // initial list location
stoInstruction DAT 300 // STO instruction
ldaInstruction DAT 500 // LDA instruction
one DAT 1 // one (constant)
zero DAT 0 // zero (constant)
list DAT
<script src="https://cdn.jsdelivr.net/gh/trincot/[email protected]/lmc.js"></script>