Dies ist mein Code für jetzt, aber es kann nur 1-stellige Zahlen. Es ist für den Unterricht und ich weiß nicht, wie es mehrere Ziffern machen kann. Ich bin noch ein Anfänger bei ASM Programmierung und ich wollte um Hilfe bitten, wie man mehrere Ziffern macht.Wie Sie mathematische Operationen mit mehreren Ziffern in ASM ausführen
.model small
.stack 4096h
.data
msg1 db 'Choose an operation:$'
msg2 db 'a. Addition$'
msg3 db 'b. Subtraction$'
msg4 db 'c. Multiplication$'
msg5 db 'd. Division$'
msg6 db 'You choose Addition.$'
msg7 db 'You choose Subtraction.$'
msg8 db 'You choose Multiplication.$'
msg9 db 'You choose Division.$'
msg11 db 'First number:$'
msg12 db 'Second number:$'
msg13 db 'Operation:$'
msg14 db 'Answer:$'
msg15 db 'Enter two numbers then choose the operation.$'
op1 db ?
op2 db ?
res1 db ?
res2 db ?
.code
start:
mov ax,@data
mov ds,ax
_start:
mov ah, 09h
lea dx, msg15
int 21h
call newline
mov ah, 09h
lea dx, msg11
int 21h
mov ah, 01h
int 21h
sub al,30h
mov op1, al
call newline
mov ah, 09h
lea dx, msg12
int 21h
mov ah, 01h
int 21h
sub al,30h
mov op2, al
call newline
mov ah, 09h
lea dx, msg1
int 21h
call newline
mov ah, 09h
lea dx, msg2
int 21h
call newline
mov ah, 09h
lea dx, msg3
int 21h
call newline
mov ah, 09h
lea dx, msg4
int 21h
call newline
mov ah, 09h
lea dx, msg5
int 21h
call newline
mov ah, 09h
lea dx, msg13
int 21h
mov ah, 01h
nt 21h
cmp al, 'a'
je _add
cmp al, 'b'
je _sub
cmp al, 'c'
je _mul
cmp al, 'd'
je _div
_add:
call newline
lea dx, msg6
mov ah, 09h
int 21h
call newline
mov ax, 0
mov al, op1
add al, op2
cmp al,0ah
jge _twodig
mov res2,al
jmp _ans
_sub:
call newline
lea dx, msg7
mov ah, 09h
int 21h
call newline
mov ax, 0
mov al, op1
sub al, op2
mov res2,al
mov res1,ah
jmp _ans
_mul:
call newline
lea dx, msg8
mov ah, 09h
int 21h
call newline
mov ax,0
mov bx,0
mov al, op1
mov bl, op2
mul bx
mov res2,al
jmp _mans
_div:
call newline
lea dx, msg9
mov ah, 09h
int 21h
call newline
mov ax,0
mov bx,0
Mov dx,0
mov al, op1
mov bl, op2
div bx
mov res1, dl
mov res2, al
jmp _dans
_twodig:
sub al, 0ah
mov ah,01h
add ax, 3030h
mov bx, ax
lea dx, msg14
mov ah, 09h
int 21h
mov dl,bh
mov ah, 02h
int 21h
mov dl,bl
mov ah, 02h
int 21h
jmp _loop
_ans:
lea dx, msg14
mov ah, 09h
int 21h
mov al, res2
add al, 30h
mov dl,al
mov ah, 02h
int 21h
jmp _loop
_mans:
mov dx, 0
mov bl,0ah
div bx
mov ah,dl
add ax, 3030h
mov bx,ax
cmp bl,30h
je _ans
lea dx, msg14
mov ah, 09h
mov dl,bl
mov ah, 02h
int 21h
mov dl,bh
mov ah, 02h
int 21h
jmp _loop
_dans:
lea dx, msg14
mov ah, 09h
int 21h
mov al, res2
mov ah, res1
add ax, 3030h
mov bx, ax
mov dl,bl
mov ah, 02h
int 21h
mov dl,'r'
mov ah, 02h
int 21h
mov dl,bh
mov ah, 02h
int 21h
jmp _loop
_loop:
mov ah,4ch
int 21h
newline proc
mov dl, 0ah
mov ah, 02h
int 21h
mov dl, 0d
mov ah, 02h
int 21h
ret
newline endp
end start
Ich verstehe nicht, warum Sie das so kompliziert gemacht haben. Lesen Sie eine Zahl von der Konsoleneingabe, speichern Sie sie in einem Register, lesen Sie eine andere Zahl von der Konsoleneingabe, speichern Sie sie in einem zweiten Register, und dann "ADD" (oder "SUB" oder "MUL" oder "DIV") diese Register . Abhängig von der Größe der verwendeten Register erhalten Sie Unterstützung für 8-Bit-, 16-Bit-, 32-Bit- oder 64-Bit-Werte. Vielleicht ist das Problem, dass Sie nur 1 Zeichen pro Zeit von der Konsoleneingabe lesen? Erwägen Sie die Verwendung von gepufferten Eingaben oder eine andere Möglichkeit, dass der Benutzer angibt, dass die Eingabe beendet ist. –
@CodyGray Die Änderung des zu verwendenden Codes [gepufferte Eingabe] (http://spike.scu.edu.au/~barry/interrupts.html#ah0a) ist möglicherweise keine große Herausforderung. Aber wenn er sich seinen Code anschaut, scheint er keine echte "atoi" -Funktion zu haben und macht nur "sub al, 30h". Die Schritte zum Drehen der gepufferten Kette "1234" in ein Register können für einen Neuling nicht offensichtlich sein. Horlisch, hilft es, an "1234" als 4 + (3 * 10) + (2 * 100) + (1 * 1000) zu denken? Ja, das wäre ein Schmerz zu schreiben, aber ich glaube nicht, dass Sie irgendwelche Werkzeuge in asm oder dos finden werden, die das für Sie tun werden. Macht Sie wirklich die libs von c schätzen, oder? –