두 개의 두 자리 숫자 (사용자가 숫자를 입력)를 더하거나 뺄 필요가있는 대표팀을위한 프로젝트가 있습니다. 나는 일을 더했다. 빼기는 십진 자릿수가 정확하지만 한 자릿수 자리에는 홀수 문자를 제공합니다. 내가 사용하는어셈블리 두 자리 숫자 빼기
:
- TASM과 TLINK
- 86
- 윈도우 XP (가상 박스)
코드 :
.MODEL SMALL
.STACK 100h
.DATA
startMsg DB 13,10,'1.) Add ',10,'2.) Subtract',10,'3.) Exit',10,10,'Select a function: $'
integer1Msg DB 13,10,10,'Enter the first integer: $'
integer2Msg DB 13,10,'Enter the second integer: $'
errorOccuredMsg DB 13,10,13,10,'An error occured, please try again! $'
sumMsg DB 13,10,10,'The sum is: $'
subMsg DB 13,10,10,'The differance is: $'
gotNum DB 0
func DB 0
.CODE
start:
mov ax,@data
mov ds,ax
mov gotNum, 0 ; initialize var
;display & get the selection
mov ah,09
mov dx,OFFSET startMsg
int 21h
mov ah,01
int 21h
mov func,al
;check what the selection was
cmp func,'1'
je additionIntermediate
cmp func,'2'
je subtractionIntermediate
cmp func,'3'
je exit
exit:
mov ah,4ch
int 21h
getNum1:
;get the first integer (1st digit)
mov ah,09
mov dx,OFFSET integer1Msg
int 21h
mov ah,01
int 21h
;check that input is a number
cmp al, 30h ;0 (ASCII 48)
jl errorIntermediate
cmp al, 39h ;9 (ASCII 57)
jg errorIntermediate
mov bh,al
sub bh,30h
;get the first integer (2nd digit)
mov ah,01
int 21h
;check that input is a number
cmp al, 30h ;0 (ASCII 48)
jl errorIntermediate
cmp al, 39h ;9 (ASCII 57)
jg errorIntermediate
mov bl,al
sub bl,30h
jmp getNum2
additionIntermediate:
jmp addition
subtractionIntermediate:
jmp subtraction
errorIntermediate:
jmp errorOccured
getNum2:
;get the second integer
mov ah,09
mov dx,OFFSET integer2Msg
int 21h
mov ah,01
int 21h
;check that input is a number
cmp al, 30h ;0 (ASCII 48)
jl errorOccured
cmp al, 39h ;9 (ASCII 57)
jg errorOccured
mov ch,al
sub ch,30h
;get the second integer
mov ah,01
int 21h
;check that input is a number
cmp al, 30h ;0 (ASCII 48)
jl errorOccured
cmp al, 39h ;9 (ASCII 57)
jg errorOccured
mov cl,al
sub cl,30h
mov gotNum,1
cmp func,'1'
je addition
cmp func,'2'
je subtraction
cmp func,'3'
je errorOccured
getNumIntermediate:
jmp getNum1
addition:
cmp gotNum,0
je getNumIntermediate
;add the two numbers and adjust for addition
mov ah,0
add bx,cx
aaa
or bx,3030h
;display result
mov ah,09
mov dx,OFFSET sumMsg
int 21h
mov dl,bh
mov ah,02
int 21h
mov dl,bl
mov ah,02
int 21h
;return to beginning
jmp start
errorOccured:
lea dx, errorOccuredMsg
mov ah,09
int 21h
jmp start
subtraction:
cmp gotNum,0
je getNumIntermediate
;determine which subtraction to use
cmp bx,cx
jg subtractionPos
cmp bx,cx
jl subtractionNeg
subtractionPos: ;integer1 is larger than integer2
;subtract
sub bx,cx
aas
or bx,3030h
;display result
mov ah,09
mov dx,OFFSET subMsg
int 21h
mov dl,bh
mov ah,02
int 21h
mov dl,bl
mov ah,02
int 21h
;return to beginning
jmp start
subtractionNeg: ;integer2 is larger than integer1
;subtract
sub cx,bx
aas
or cx,3030h
;display result
mov ah,09
mov dx, OFFSET subMsg
int 21h
mov ah,06
mov dl,2dh ;displays the negative sign
int 21h
mov dl,ch
mov ah,02
int 21h
mov dl,cl
mov ah,02
int 21h
;return to beginning
jmp start
end start
나는 매우 신중하기 때문에 어떤 충고라도 잘 될 것입니다.
EDIT
;subtract
sub bx,cx
mov ch,bh ;store the value of bh
xchg bx, ax
mov bl,0Ah
div bl
xchg ah, al
xchg ax, bx
mov bh,ch ;restore the value of bh
or bx,3030h
위법은 아닙니다. 당신은 새로운 사람입니다. 아마도 우리 모두는 어딘가에서 시작해야합니다.하지만 이것은 매우 혼란 스럽습니다. 어셈블리에서 함수에서 코드를 구성 할 필요는 없지만 일반적으로 좋은 아이디어입니다. 또한'aaa'와'aas'를 잘못 사용했기 때문에 그들이하는 일을 재검토해야합니다. (아마도 이것은 진짜 문제 일 것입니다 만, 말하기는 조금 어렵습니다). – harold
@harold 코드를 구성하려고했지만 jmp를 사용할 때 "범위를 벗어난"오류가 발생합니다. 그래서 코드를 조금 섞어서 사용해야했지만 '동일한'코드를 함수에 유지하려고했습니다. 그리고 나는 aaa와 aas에 다시 읽혀질 것이다. 그래도 조언 주셔서 감사합니다. –
함수가 다시 점프 할 위치를 결정하는 전역 상태가 아닌'call'과'ret'를 시도하십시오 – harold