我正在尝试为函数F =(A& B)XOR(C& D')创建一个真值表。我使用的是英特尔汇编语言,整个代码必须相当短。
我一直遇到的主要问题是循环操作变量A,B,C和D来创建真值表的所有可能输入。通常这样做的方式就是像变量一样,每个变量都是4位二进制数,并从0到15计数,实现每个可能的输入。我只有4个寄存器堆栈和RAM内存来实现这一点,所以我将每个变量分配给一个寄存器,并使用ram来存储计数器和表的输出。我想不出一个用循环来做这个的好方法,但是我必须使用一个,因为我使用的模拟器只要我已经成为代码就无法使用代码。 (它是256字节)
这是代码,程序60的子程序是我想要压缩的部分。我不必实际输出一个真值表,只知道有多少“Minterm”(1的输出)由它产生。
;MAIN
MOV AL,0 ;INITIALIZING VARIABLES
MOV BL,0
MOV CL,0
MOV DL,0
MOV [20],AL ;INITIALIZE CURRENT NUMBER OF MINTERMS WITH 0
MOV [21],AL ;INITIALIZE COUNTER WITH 0
LOOP: CALL 30
CALL 60
MOV [21],CL
CMP CL,15
JS LOOP
CALL E0
MOV AL,[20] ;MOVE NUMBER OF MINTERMS TO AL
MOV [FF],AL ;OUPUT NUMBER OF MINTERMS TO VDU(LOCATIONS [C0] TO [FF])
;PROCEDURE 30, IMPLEMENTS THE GIVEN BOOLEAN FUNCTION
ORG 30 ;WRITE CODE BEGINNING AT [30]
PUSH DL ;CURRENT VALUE OF D
PUSH CL ;CURRENT VALUE OF C
PUSH AL ;CURRENT VALUE OF A, IMPLEMENTING FUNCTION WON'T CHANGE D
AND AL,BL ;PERFORMS (AL AND BL), STORES VALUE IN AL
NOT DL ;INVERSE OF DL STORED IN DL
AND CL,DL ;PERFORMS (CL AND DL), STORES VALUE IN CL
XOR AL,CL ;PERFORMS (AL XOR CL), STORES VALUE IN AL
MOV BL,[20] ;MOVES CURRENT NUMBER OF MINTERMS TO BL
ADD AL,BL ;ADD TO CURRENT NUMBER OF MINTERMS IF MINTERM RESULTED FROM FUNCTION
MOV [20],AL ;STORE NEW CURRENT NUMBER OF MINTERMS
MOV CL,[21] ;MOVE COUNTER TO CL
INC CL ;INCREMENT CL, INCREMENTING COUNTER
MOV [21],CL ;MOVE NEW VALUE OF COUNTER
POP AL ;RESTORE PREVIOUS VALUE OF D
POP CL ;RESTORE PREVIOUS VALUE OF C
POP DL ;RESTORE PREVIOUS VALUE OF A
RET
;PROCEDURE 60, MANIPULATES VARIABLES TO IMPLEMENT FULL TRUTH TABLE
ORG 60 ;WRITE CODE BEGINNING AT [60]
MOV CL,[21] ;MOVE COUNTER VALUE TO CL REGISTER
CMP CL,1 ;NEED TO MAKE THIS INTO A LOOP
JZ FIRST
CMP CL,2
JZ SECOND
CMP CL,3
JZ FIRST
CMP CL,4
JZ FOURTH
CMP CL,5
JZ FIRST
CMP CL,6
JZ SECOND
CMP CL,7
JZ FIRST
CMP CL,8
JZ EIGHTH
CMP CL,9
JZ FIRST
CMP CL,10
JZ SECOND
CMP CL,11
JZ FIRST
CMP CL,12
JZ FOURTH
CMP CL,13
JZ FIRST
CMP CL,14
JZ SECOND
CMP CL,15
JZ FIRST
JMP LAST
FIRST:
MOV AL,1
JMP LAST
SECOND:
MOV AL,0
MOV BL,1
JMP LAST
FOURTH:
MOV AL,0
MOV BL,0
MOV CL,1
JMP LAST
EIGHTH:
MOV AL,0
MOV BL,0
MOV CL,0
MOV DL,1
JMP LAST
LAST:
RET
FINISH:
ORG E0 ;WRITE CODE BEGINNING AT E0(OUTPUTS TO VDU)
DB "The number of Minterms in F is" ;OUTPUT STATEMENT
RET
END
答案 0 :(得分:2)
简单地说:
mov cl, 0 ; CL contains DCBA bits
L:
mov bl, cl ; BL contains DCBAb
and bl, 101b ; BL contains 0C0Ab
mov al, cl ; AL contains DCBAb
shr al, 1 ; AL contains 0DCBb
and al, 101b ; AL contains 0D0Bb
and bl, al ; BL contains 0(C&D)0(A&B)b
mov al, bl ; AL contains 0(C&D)0(A&B)b
and bl, 1 ; BL contains 000(A&B)b
shr al, 2 ; AL contains 000(C&D)b
xor al, bl ; AL contains 000((C&D) XOR (A&B))b
inc cl ; increment DCBA as a 4-bit number
cmp cl, 16 ; all 16 combinations?
jne L ; not yet, repeat
答案 1 :(得分:0)
xor cx,cx ; init counter register
repeatLoop:
mov al,cx ; bits a,b,c and d in lowest 4 nibbles
call processtruthtable
inc cx
cmp cx,16 ; 4bits so 16 possibilities
jl repeatLoop
jmp exitProgram
processtruthtable:
....
....
....
....
ret
exitProgram:
// whatever OS you use, use the exit INT call
for dos it's INT21/4C
for linux it's INT80/01
part after / is to be stored in AH
如果我错了,请纠正我