assembly 如何在汇编语言中定位字符

qzwqbdag  于 2023-05-07  发布在  其他
关注(0)|答案(1)|浏览(248)

我想找到一个字符通过索引使用汇编语言,但它不会打印该字符的索引。
下面是我的代码:

.model small
.stack 100h
.data   

    CR          equ 13d
    LF          equ 10d
    
    intro db CR, LF, "Assemby Language Programming Lab Project $"
    intro1 db CR, LF, "Project Name: Number Hunt $"
    intro2 db CR, LF, "By: Floren Magulod (21-020002) & Shelzy Bareng (21-020035) $"
    line db CR, LF, "------------------------------$"
    
    opt1 db CR, LF, "1.Reverse Array $"
    opt2 db CR, LF, "2.Calculator $"
    opt3 db CR, LF, "3.Hunt the number$" 
    
    prompt db CR, LF, "Choose an option: $"
    
    arr1 db CR, LF, "Enter Array size (0-9): $"
    arr2 db CR, LF, "Enter Elements (0-9): $"
    arr3 db CR, LF, "Reverse form: $" 
    
    cal1 db CR, LF, "Enter 1st number: $"
    cal2 db CR, LF, "Enter 2nd number: $"
    
    choice1 db CR, LF, "1.Addition $"
    choice2 db CR, LF, "2.Subtraction $"
    choice3 db CR, LF, "3.Multiplication $"
    choice4 db CR, LF, "4.Division $"   
    
    res1 db CR, LF, "Result: $"
    
    choice5 db CR, LF, "1.Odd $"
    choice6 db CR, LF, "2.Even $"
    
    input1 db CR, LF, "Sum: $"
    odd db CR, LF, "Odd numbers: $"
    even db CR, LF, "Even numbers: $"  
    
    
    
    find1 db CR, LF, "Enter a character to find: $"
    find2 db CR, LF, "Character is found at index: $"
    find3 db CR, LF, "Character is not found $"
   

    retry db CR, LF, 'Retry [y/n] ? ' ,'$'
    
    choice db 0
    time dw 0
    d db 10 dup(?)
    sum db 0
    num1 db 0
    num2 db 0
    rem db 0  
    char db 10 dup(?)

  
.code
    main proc
        mov ax, @data
        mov ds, ax
        
        mov ah, 09h         
        lea dx, intro
        int 21h
   
        mov ah, 09h         
        lea dx, intro1
        int 21h
      
        mov ah, 09h         
        lea dx, intro2
        int 21h 
        
        mov ah, 09h         
        lea dx, line
        int 21h
        
    start:
        mov ah, 09h         
        lea dx, opt1
        int 21h
        
        mov ah, 09h         
        lea dx, opt2
        int 21h
        
        mov ah, 09h         
        lea dx, opt3
        int 21h
        
        mov ah, 09h         
        lea dx, line
        int 21h
        
        mov ah, 09h         
        lea dx, prompt
        int 21h
        
    while:
        mov ah,1
        int 21h
        sub al,48
        mov choice,al
    
        cmp choice,2
        je calcu
        jl reverse
        jg even_odd
        
        cmp choice, 3
        jg hunt
        
    
    calcu:
        mov ah, 09h
        lea dx, cal1
        int 21h
        
        mov ah,1
        int 21h
        sub al,48
        mov num1,al
        
        mov ah, 09h
        lea dx, cal2
        int 21h        
       
        mov ah,1
        int 21h
        sub al,48
        mov num2,al
        
        mov ah, 09h         
        lea dx, line
        int 21h  
 
        
        mov ah, 09h
        lea dx, choice1
        int 21h 
        
        mov ah, 09h
        lea dx, choice2
        int 21h
        
        mov ah, 09h
        lea dx, choice3
        int 21h
        
        mov ah, 09h
        lea dx, choice4
        int 21h
        
        mov ah, 09h         
        lea dx, line
        int 21h
        
        mov ah, 09h         
        lea dx, prompt
        int 21h
        
        mov ah,1
        int 21h
        sub al,48
        mov choice, al
        
        cmp choice,2
        je subs
        jl addi
        jg multi  
        
        cmp choice,3
        je multi
        jl subs
        jg divi
           
    addi:
        mov al,num1
        add al,num2
    
        mov ah, 09h
        lea dx, res1
        int 21h
    
        mov ah,0
        mov bl,10
        div bl
        mov rem,ah
        
        mov dl,al
        add dl,48
        mov ah,2
        int 21h
        
        mov dl,rem
        add dl,48
        mov ah,2
        int 21h
        jmp again
        
   subs:
        mov ah, 09h
        lea dx, res1
        int 21h
        
        mov al,num1
        sub al,num2
        add al,48
                
    
        mov dl,al
        mov ah,2
        int 21h
        
        jmp again 
    
   
    multi:    
        mov ah, 09h
        lea dx, res1
        int 21h
        
        mov al,num1
        mul num2
    
        mov ah,0
        mov bl,10
        div bl
        mov rem,ah 
        
        
        mov dl,al
        add dl,48
        mov ah,2
        int 21h
        
        mov dl,rem
        add dl,48
        mov ah,2
        int 21h
        jmp again
    
    
    divi: ;;wrong fixed it
        mov ah, 09h
        lea dx, res1
        int 21h
        
        mov ah,0
        mov al,num1
        div num2
        add al,48
    
        mov dl,al
        mov ah,2
        int 21h
        jmp again 
        
 
    reverse:
        mov ah, 09h
        lea dx, line
        int 21h
        
        mov ah, 09h         
        lea dx, arr1
        int 21h
        
        mov ah,1
        int 21h
        sub al,48
        mov ah,0
        mov time,ax
       
        mov ah, 09h
        lea dx, arr2
        int 21h 
        
        mov cx,time
        mov bx,0
    
    val_store:
    
        mov ah,1
        int 21h
        sub al,48
        mov d[bx],al
        push ax
        add sum,al
        inc bx
        loop val_store
    
        mov ah, 09h
        lea dx, arr3
        int 21h
    
        mov cx,time
          
    
    rev:
        pop dx
        add dl,48
        mov ah,2
        int 21h
        
        loop rev
        jmp again
        
    even_odd:
        mov ah, 09h
        lea dx, line
        int 21h
        
        mov ah, 09h         
        lea dx, arr1
        int 21h   
       
        
        mov ah,1
        int 21h
        sub al,48
        mov ah,0
        mov time,ax 
        
        mov ah, 09h
        lea dx, arr2
        int 21h
               
        mov cx,time
        mov bx,0
   

    value_store:
    
        mov ah,1
        int 21h
        sub al,48
        mov d[bx],al
        push ax
        add sum,al
        inc bx
        loop value_store
        
        mov cx,time
        
        mov ah, 09h
        lea dx, choice5
        int 21h
        
        mov ah, 09h
        lea dx, choice6
        int 21h
        
        mov ah, 09h         
        lea dx, line
        int 21h
        
        mov ah, 09h         
        lea dx, prompt
        int 21h
        
        mov ah,1
        int 21h
        sub al,48
       
        
        cmp al, 049
        je odd_num
        jl even1
        
        
          
            
    odd_num:
    
        mov ah, 09h
        lea dx, odd
        int 21h
        
    
    even1:
        mov ah, 09h
        lea dx, even
        int 21h
        
        jmp hunt
    
    
    summ:
        
    hunt:
        
        mov ah, 09h
        lea dx, find1
        int 21h
        
        mov ah, 1
        int 21h
        
 
        mov al, 1
        lea bx, d 
        
    l2:
        cmp [bx],al
        je PrintFound
    
        cmp al, 10
        je NotFound
   
        inc dx
        jmp l2
    
    
    
    PrintFound:
        
        lea dx, find2
        mov ah, 9
        int 21h
        
        mov dx, 0
        
        mov bl, al
        add al, 48
        mov ah, 2
        int 21h
    
        jmp again
    
    NotFound:
        
        lea dx, find3
        mov ah, 9
        int 21h
        
        jmp again
        
    
 
    again:
        mov ah, 09h
        lea dx, line
        int 21h 
        
        mov ah, 09h
        lea dx, retry
        int 21h
        
        mov ah, 1h
        int 21h
        
        cmp al, 110
        je stop
        
        cmp al, 121
        je start
        
        jmp again
    
    stop:
        mov ax, 4c00h
        int 21h

我希望它找到我正在寻找的字符索引,然后打印它的索引。

laawzig2

laawzig21#

opt1 db CR, LF, "1.Reverse Array $"
opt2 db CR, LF, "2.Calculator $"
opt3 db CR, LF, "3.Hunt the number$"

您的主菜单有3个选项,但下面的代码似乎期望有4个选项!

cmp choice,2
je calcu
jl reverse
jg even_odd

cmp choice, 3
jg hunt

问题是条件跳转的级联已经覆盖了cmp choice,2指令的所有可能结果:如果它不相等,也不小,那么它就必须更大。因此,cmp choice, 3指令永远不会执行。

方案一

保留3个菜单选项(并可能从程序中删除 even_odd 代码),并像这样调度:

cmp choice,2
jl  reverse
je  calcu
jg  hunt

方案二

添加第4个菜单选项并相应地派单:

opt1 db CR, LF, "1.Reverse Array $"
opt2 db CR, LF, "2.Calculator $"
opt3 db CR, LF, "3.Even Odd$"
opt4 db CR, LF, "4.Hunt the number$"

...

while:
  mov ah, 01h
  int 21h
  sub al, 48
  jz  while       ; REDO on "0"
  mov choice, al
  cmp al, 2
  jb  reverse     ; "1"
  je  calcu       ; "2"
  cmp al, 4
  jb  even_odd    ; "3"
  je  hunt        ; "4"
  ja  while       ; REDO on anything else

相关问题