当前位置: 动力学知识库 > 问答 > 编程问答 >

dos - How to make my Assembly Code WOrk

问题描述:

I'm Creating a Menu in Assembly Language. the Operations Work but the CMP and Jump commands dont. But I can't make it work .. Any Suggestion I'm New in Assembly and just experimenting around with it .. Please Help me

#make_COM#

; COM file is loaded at CS:0100h

ORG 100h

jmp start

msg9 db "Enter 1: Addition "

db 10,13, "Enter 2: Subtraction "

db 10,13, "Enter 3: Multiplication: $"

num9 db 0

start:

mov ax, 3

int 10h

lea dx, msg9

mov ah, 9

int 21h

mov ah, 1

int 21h

sub al,30h

mov num9,al

mov bl,al

cmp bl,4

jl addition

je subtraction

jg multiplication

;--------------------------------------------------------------------------------------------

multiplication:

msg1 db "Enter 1st number: $"

msg2 db 10,13, "Enter 2nd number: $"

msg3 db 10,13, "SUM: $"

num1 db 0

num2 db 0

num3 db 0

num4 db 0

mov ax, 3

int 10h

lea dx, msg1

mov ah, 9

int 21h

mov ah, 1

int 21h

sub al,30h

mov num1,al

lea dx, msg2

mov ah, 9

int 21h

mov ah, 1

int 21h

sub al,30h

mov num2,al

lea dx, msg3

mov ah, 9

int 21h

mov al,num1

mov bl,num2

mul bl

mov bl,al

cmp bl,9

jl less

jl greater

greater:

xor ax,ax

mov al,bl

mov bl,10

div bl

mov bl,ah

mov dl,al

or dl,30h

mov ah,2

int 21h

mov dl,bl

or dl,30h

mov ah,2

int 21h

less:

mov dl,al

or dl,30h

mov ah,2

int 21h

int 20h

;-----------------------------------------------------------------

addition:

msg4 db "Enter 1st number: $"

msg5 db 10,13, " Enter 2nd number $"

msg6 db 10,13, " Sum: $"

num5 db 0

num6 db 0

num7 db 0

num8 db 0

mov ax,3

int 10h

lea dx, msg4

mov ah, 9

int 21h

mov ah,1

int 21h

sub al,30h

mov num5,al

mov ah,1

int 21h

sub al,30h

mov num6,al

lea dx, msg5

mov ah, 9

int 21h

mov ah,1

int 21h

sub al,30h

mov num7,al

mov ah,1

int 21h

sub al,30h

mov num8,al

lea dx, msg6

mov ah, 9

int 21h

mov al,num1

add al,num3

mov bl,num2

add bl,num4

mov dl,al

or dl,30h

mov ah,30h

mov ah,2

int 21h

mov dl,bl

or dl,30h

mov ah,30h

mov ah,2

int 21h

;----------------------------------------------------------------------------------------------

subtraction:

msg11 db "Enter 1st number: $"

msg12 db 10,13, "$"

msg13 db 10,13, "Enter 2nd number: $"

msg14 db 10,13, "Difference : $"

num15 db 0

num16 db 0

num17 db 0

num18 db 0

mov ax,3

int 10h

lea dx, msg11

mov ah, 9

int 21h

mov ah,1

int 21h

sub al,30h

mov num15,al

mov ah,1

int 21h

sub al,30h

mov num16,al

lea dx, msg14

mov ah, 9

int 21h

lea dx, msg12

mov ah, 9

int 21h

mov ah,1

int 21h

sub al,30h

mov num17,al

mov ah,1

int 21h

sub al,30h

mov num18,al

lea dx, msg14

mov ah, 9

int 21h

mov al,num15,num16

mov bl,num17,num18

cmp al,bl

JG positive

JL negative

positive:

mov al,num15

sub al,num17

mov bl,num16

sub bl,num18

mov dl,al

or dl,30h

mov ah,30h

mov ah,2

int 21h

mov dl,bl

or dl,30h

mov ah,30h

mov ah,2

int 21h

int 20h

negative:

mov dl,'-'

mov ah,2

int 21h

mov al,num15

sub al,num17

mov bl,num16

sub bl,num18

mov dl,al

or dl,30h

mov ah,30h

mov ah,2

int 21h

mov dl,bl

or dl,30h

mov ah,30h

mov ah,2

int 21h

lea dx, msg14

mov ah, 9

int 21h

int 20h

网友答案:

You need to compare with 2 since your menu use 1, 2, and 3.

cmp bl,4
jl addition
je subtraction
jg multiplication

You need to jump over these msg_ and num_ lines. (msg3 should read PRODUCT)

multiplication:
jmp multiplication_bis
msg1 db "Enter 1st number:  $"
...
multiplication_bis:

To separate the 1 digit numbers from the 2 digit numbers compare with 10.

cmp bl,10
jl less
jl greater  ; <-- useless, the code can fall through

The greater block must not fall through in the less block.

int 20h  ; <-- add this one
less:

You need to jump over these msg_ and num_ lines.

addition:
jmp addition_bis
msg4 db "Enter 1st number:  $"
...
addition_bis:

You use the wrong data. I suspect that you even wanted to use 2 digit input. Now you are just adding separate 1 digit inputs.

mov al,num1  ; should be num5
add al,num3  ; should be num7
mov bl,num2  ; should be num6
add bl,num4  ; should be num8

You need to jump over these msg_ and num_ lines.

int 20h  ; You forgot to cut off the *addition* block
subtraction:
jmp subtraction_bis
msg11 db "Enter 1st number:  $"
...
subtraction_bis:

The wrong messages get selected.

lea dx, msg14  ; should be msg13
mov ah, 9
int 21h 
lea dx, msg12  ; is useless!
mov ah, 9
int 21h

This does not exist in X86 assembly.

mov al,num15,num16  ; loose ,num16
mov bl,num17,num18  ; loose ,num18
cmp al,bl
JG positive
JL negative

You need to swap the operands to get a meaningful result.

negative:
mov dl,'-'
mov ah,2
int 21h
mov al,num15  ; should be num17
sub al,num17  ; should be num15
mov bl,num16  ; should be num18
sub bl,num18  ; should be num16

The program ends with a superfluous display of msg14. Unintended?

网友答案:

Your addition, subtraction and multiplication sections all start with a small data block, but your code that branches to them branches to the start of the data block, and then attempts to execute the text it finds there. Separate the data blocks from the code and place your labels at the appropriate places in the code.

;Your instruction `jl addition` branches to here:

addition:

msg4 db "Enter 1st number: $"
msg5 db 10,13, " Enter 2nd number $"
msg6 db 10,13, " Sum: $"

num5 db 0
num6 db 0
num7 db 0
num8 db 0

; It should branch to here

mov ax,3
int 10h 
分享给朋友:
您可能感兴趣的文章:
随机阅读: