问题
I've been given an assingment to make a calculator in 8086 assembly that will add, substract, multiply and divide big decimal numbers. These numbers can be 30 digits long at most. I've used 3 arrays to house these numbers (num1, num2, result). I'm stuck at the addition, because every time I run the program, it displays that the result array is empty (it shows a plus sign and 60 zeroes) can you tell what is wrong with my code?
ADDER:
MOV BP, offset num1
MOV SI, offset num2
MOV DI, offset result
MOV CL, byte ptr CS:[BP]
MOV CH, byte ptr CS:[SI]
CMP CL, CH
JZ addgood
CMP CL, '-'
JNZ svers
MOV byte ptr CS:[BP], '+'
MOV SI, offset num1
MOV BP, offset num2
JMP SUBSTRACTOR
svers:
CMP CH, '-'
MOV byte ptr CS:[SI], '+'
JMP SUBSTRACTOR
addgood:
lastdigit1:
MOV AL, byte ptr CS:[BP]
CMP AL, 127
JZ gotlastdigit1
INC BP
JMP lastdigit1
gotlastdigit1:
lastdigit2:
MOV AL, byte ptr CS:[SI]
CMP AL, 127
JZ gotlastdigit2
INC SI
JMP lastdigit2
gotlastdigit2:
MOV DI, offset result
ADD DI, 60
initialaddition:
CMP byte ptr CS:[BP], '+'
JNZ notover1
JMP num1end
notover1:
CMP byte ptr CS:[BP], '-'
JNZ notover2
JMP num1end
notover2:
CMP byte ptr CS:[SI], '+'
JNZ notover3
JMP num2end
notover3:
CMP byte ptr CS:[SI], '-'
JNZ notover4
JMP num2end
notover4:
MOV CH, byte ptr CS:[BP]
MOV CS:[DI], CH
MOV CH, byte ptr CS:[SI]
ADD CS:[DI], CH
DEC BP
DEC SI
DEC DI
JMP initialaddition
afteriniadd:
DEC DI
MOV CL, 127
MOV CS:[DI], CL
MOV DI, offset result
ADD DI, 60
truaddition:
MOV CH, 0
MOV CL, byte ptr CS:[DI]
CMP CL, 10
JNAE movealong
CALL pisdod
movealong:
MOV CS:[DI], CL
DEC DI
MOV CL, byte ptr CS:[DI]
CMP CL, 127
JZ isspecial
ADD CS:[DI], CH
JMP truaddition
isspecial:
CMP CH, 0
JZ afteradd
MOV CS:[DI], CH
afteradd:
DEC DI
MOV CS:[DI], 127
MOV SI, offset num1
MOV CL, CS:[SI]
MOV DI, offset result
CMP CL, '+'
JZ adplus
MOV byte ptr CS:[DI], '-'
JMP adminus
adplus:
MOV byte ptr CS:[DI], '+'
adminus:
RET
The sub-procedures
num1end:
MOV CL, '-'
CMP byte ptr CS:[SI], CL
JNZ noneed1
JMP afteriniadd
noneed1:
MOV CL, '+'
CMP byte ptr CS:[SI], CL
JNZ noneed2
JMP afteriniadd
noneed2:
MOV CL, byte ptr CS:[SI]
MOV byte ptr CS:[DI], CL
DEC DI
DEC SI
JMP num1end
;......................................
num2end:
MOV CL, '-'
CMP byte ptr CS:[BP], CL
JNZ noneed3
JMP afteriniadd
noneed3:
MOV CL, "+"
CMP byte ptr CS:[BP], CL
JNZ noneed4
JMP afteriniadd
noneed4:
MOV CL, byte ptr CS:[BP]
MOV byte ptr CS:[DI], CL
DEC DI
DEC BP
JMP num2end
pisdod:
CMP CL, 10
JGE notyet
RET
notyet:
SUB CL, 10
INC CH
JMP pisdod
And array declarations
num1 db 31 DUP(?), 127
num2 db 31 DUP(?), 127
result db 61 DUP(?), 127
With 127 serving as a marker where do the digits begin and end.
回答1:
I figured it out myself, finally. Turns out I didn't need to use the DEC DI
in afteradd:
part. That command caused the marker to be placed twice, one after another, which fooled the procedure that was supposed to print the result.
来源:https://stackoverflow.com/questions/23298622/assembly-big-numbers-calculator