代碼應該像這樣作業:從輸入中讀取一個字符并查看它是否是大寫字母 重復讀取,直到讀入一個非大寫字符,列印出所有以最新讀取的字母開頭的大寫字符,例如:A B C E f
E C B A .... 我的問題是我如何使用堆疊來確定是否每個字母都已列印,何時到達終點。如果第一次讀入 char 是一個非大寫字符,它應該列印出一條錯誤訊息
Start:
call read_char ;reading a char and comparing if it is a uppercase letter
mov ebx,eax
call read_char ;if a non uppercase letter is read in jump to print label
cmp ebx,65
jb PrintChar
cmp ebx,90
ja PrintChar
push ebx ;pushing the char onto the stack and repeating the process
jmp Start
PrintChar:
loop:
pop eax ;pop the char from the stack to the eax register and print it
call print_char
jmp loop ;repeat this process until all char are printed out (this code is not written)
MessageFail:
mov eax,msg_fail ;if the stack was empty (the first input a lovercase letter) print a fail message
call print_string
call print_nl
jmp Ende
Ende:
uj5u.com熱心網友回復:
“我如何使用堆疊來確定是否每個字母都已列印,何時到達末尾?”
您總是可以在開始之前將一個特殊字符壓入堆疊,并且在您的列印回圈期間,您可以在列印之前檢查您是否遇到這個特殊字符。如果你這樣做,然后跳轉到Ende. 我當然不會使用大寫 ASCII 范圍內的字符,因為這會導致問題。也許只需將 0x0 壓入堆疊以開始并在pop eaxin之后檢查它loop。像這樣的東西:
Start:
push 0x0 ; push 0x0 onto the stack to denote the beginning
ReadChars:
call read_char ;reading a char and comparing if it is a uppercase letter
mov ebx,eax
cmp ebx,65
jb PrintChar
cmp ebx,90
ja PrintChar
push ebx ;pushing the char onto the stack and repeating the process
jmp ReadChars
PrintChar:
loop:
pop eax ;pop the char from the stack to the eax register and print it
test eax, eax ; test if eax=0
jz Ende ; jump to end if eax was zero
call print_char
jmp loop ;repeat this process until all char are printed out (this code is not written)
MessageFail:
mov eax,msg_fail ;if the stack was empty (the first input a lovercase letter) print a fail message
call print_string
call print_nl
jmp Ende
Ende:
至于在第一個字符超出范圍時列印錯誤訊息,我認為您始終可以使用備用暫存器來存盤您是否正在對第一個字符進行操作。假設您曾經ecx存盤它。然后在Start初始化ecx到更新到0x0結束時。那么,如果是零并且你讀到的字符不在大寫ASCII范圍內,你就知道用戶輸入了一個無效的字符序列,你可以跳轉到ReadCharsecx0x1ecxMessageFail
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/406279.html
標籤:
