該程序實現了將一個字符串開頭和結尾的任意多個空格去除然后顯示字符串內容。
可以根據需要輸入不同的字符串,程序將自動識別并刪除開頭和結尾的空格。
適合本科階段學習微機原理與接口技術的同學練習參考。
question_02.png (39.32 KB, 下載次數: 68)
下載附件
2023-5-21 17:00 上傳
- ;Remove spaces at the beginning and end of a string
- DATA SEGMENT
- string DB ' a string for testing. ',0
- length DW $-string ;length of the string
- DATA ENDS
- CODE SEGMENT
- ASSUME CS:CODE, DS:DATA
- START:
- MOV AX, DATA
- MOV DS, AX
-
- LEA SI, string
-
- BEGIN: ;Set beginning pointer of the new string
- CMP [SI], ' ' ;Find spaces, skip them
- JNE NEXT
- INC SI
- JMP BEGIN
- NEXT: ;Shift DI to the end of the initial string
- LEA DI, string
- MOV AX, length ;Load length of the string to AX
- ADD DI, AX
- SUB DI, 2 ;Move DI to the end of the string
-
- THIRD: ;Shift DI to the end of the new string
- CMP [DI], ' '
- JNE LAST
- DEC DI
- JMP THIRD
-
- LAST: ;display the new string on screen
- INC DI
- MOV byte ptr[DI], '0' ;string ends with 0
- INC DI
- MOV byte ptr[DI], '
-
- MOV AH, 09H
- MOV DX, SI ;Set offset adress
- INT 21H
-
- CODE ENDS
- END START
復制代碼
|