2017-10-29 36 views
0

我是新來的大會,我想做到以下幾點:大會保存和比較字符

僞代碼:

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值

+0

您已經做了比較。要存儲一個值,只需選擇一個未被銷燬的寄存器即可。 – Jester

+3

'loopback'在這種情況下不是一個好的選擇,你想'jmp back'總是跳到那裏,''cx'變成零後'loop'不會跳轉(並且你沒有定義'cx',所以你在發射環境的憐憫)。和['loop'](http://x86.renejeschke.de/html/file_module_x86_id_161.html)幾乎從來都不是好的選擇,除非你打高爾夫球的代碼大小,一對指令:'dec cx''jnz label'對現代x86 CPU的性能更好。 – Ped7g

+0

或者在這種情況下,'jne back',而不是有條件地跳過一個無條件的循環分支。 –

回答

1

我添加了一些代碼的說明來解決你的問題。我已經成功測試了它們。還有一個顯示ASCII碼的新程序。請注意,某些ASM編譯器使用顯示器作爲保留字(我在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