程序集保存和比较字符

时间:2017-10-29 00:06:50

标签: assembly dosbox emu8086

我是大会新手,我正在努力做到以下几点:

伪代码:

 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值

1 个答案:

答案 0 :(得分: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