【问题标题】:Assembly save and compare characters程序集保存和比较字符
【发布时间】:2017-10-29 00:06:49
【问题描述】:

我是 Assembly 的新手,我正在尝试执行以下操作:

伪代码:

 loop:
      input
      if(input == $)
        end loop
      else if(input < savedInput)
        savedInput = input
    ;
    print savedInput

基本上,它是一个恒定循环,用于检测用户输入并将保存的输入的 ASCII 值与新输入进行比较,如果新输入低于则替换保存的输入。如果输入等于 $ 则结束循环并打印保存的输入。

这是我的代码

.MODEL SMALL
.STACK 100h
.DATA
    insertMsg DB 13, 10, 'Introduce un dato: ', 13, 10, '$'

.CODE
    main:
        mov ax,@data
        mov ds,ax                   ; Set DS to point to the data segment
        mov dx,OFFSET insertMsg     ; Point to the insertMsg
    back:
        mov ah,9                    ; DOS: print string: Service 21h, 09h
        int 21h                     ; Display inputMsg
        mov ah,1                    ; DOS: get character: Service 21h, 01h
        int 21h                     ; Get a single-character response
        cmp al,'$'                  ; if character equals $
        je display                  ; goto display
        loop back                   ; loop back

    display:
        mov ah,9            ;DOS: print string: Service 21h, 09h
        int 21h             ;display input
        mov ah,4Ch          ;DOS: terminate program: Service 21h, 4Ch
        mov al,0            ;return code will be 0
        int 21h             ;terminate the program
    end main

问题是我不知道如何保存和比较 ASCII 值

【问题讨论】:

  • 您已经在进行比较。要存储一个值,只需选择一个未销毁的寄存器。
  • loop back 在这种情况下不是一个好的选择,你想做jmp back 总是跳到那里,loopcx 变为零后不会跳(你不会定义cx,这样你就可以随意使用启动环境了)。而loop 几乎从来都不是好的选择,除非您对代码大小很感兴趣,否则这对指令:dec cx jnz label 在现代 x86 CPU 上的性能更好。
  • 或者在这种情况下,jne back 而不是有条件地向前跳转到无条件循环分支。

标签: assembly dosbox emu8086


【解决方案1】:

我添加了一些代码说明来解决您的问题;我已经成功地测试了它们。还有一个显示 ASCII 代码的新例程。请注意,一些 ASM 编译器使用 DISPLAY 作为保留字(我在 dispOut 中对其进行了重命名):

.MODEL SMALL
.STACK 100h
.DATA
    insertMsg DB 13, 10, 'Introduce un dato: ', 13, 10, '$'
    outputMsg DB 13, 10, 'Dato minore tra quelli inseriti: ', 13, 10, '$'
     ASCIIstr DB 13, 10, 'ASCII= ', 0, 0, 0, 13, 10, '$'

.CODE
main:
    mov  ax,@data           ; ... Set DS
    mov  ds,ax              ; ... to point to the data segment
    mov  dx,OFFSET insertMsg; Point to the insertMsg
    mov  bx,OFFSET ASCIIstr ; Point to the ASCIIstr
    mov  cl,255             ; Set SAVED-INPUT to max-ASCII

back:
    mov  ah,9               ; DOS: print string: Service 21h, 09h
    int  21h                ; Display inputMsg
    mov  ah,1               ; DOS: get character: Service 21h, 01h
    int  21h                ; Get a single-character response
    cmp  al,'$'             ; If character equals $ ...
    je   dispOut            ; ... goto dispOut
    cmp  al,cl              ; If char. read >= SAVED-INPUT ...
    jae  cont               ; ... skip next instruction, else ...
    mov  cl,al              ; ... save char. read (it is < SAVED-INPUT)

cont:
    call dispCode           ; Display ASCII code
    jmp  back               ; Loop back

dispOut:
    mov  dx,OFFSET outputMsg; Point to the outputMsg
    mov  ah,9               ; DOS: print string: Service 21h, 09h
    int  21h                ; Display outputMsg
    mov  dl,cl              ; Load SAVED-INPUT into DL's reg.
    mov  ah,2               ; DOS: display output: Service 21h, 02h
    int  21h                ; Write a single-character
    call dispCode           ; Display ASCII code
    mov  ah,4Ch             ; DOS: terminate program: Service 21h, 4Ch
    mov  al,0               ; Return code will be 0
    int  21h                ; Terminate the program

dispCode:                   ; Display an ASCII code
    ; AL: ASCII code
    ; BX: OFFSET ASCIIstr
    mov  ch,10              ; Load 10 into CH's reg.
    xor  ah,ah              ; AX contains the ASCII code
    div  ch                 ; AH contains the last significant digit
    add  ah,'0'             ; Converts AH into ASCII digit
    mov  ds:[bx+11],ah      ; Store the last significant digit in ASCIIstr
    xor  ah,ah              ; AX contains the ASCII code / 10
    div  ch                 ; AH: penult. sign. digit; AL: 1st sign. digit
    add  ah,'0'             ; Converts AH into ASCII digit
    mov  ds:[bx+10],ah      ; Store the penult. sign. digit in ASCIIstr
    add  al,'0'             ; Converts AL into ASCII digit
    mov  ds:[bx+9],al       ; Store the 1st sign. digit in ASCIIstr
    xchg dx,bx              ; Swap msg offset
    mov  ah,9               ; DOS: print string: Service 21h, 09h
    int  21h                ; Display inputMsg
    xchg dx,bx              ; Swap msg offset
    ret                     ; Return

end main

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多