標題: 微機原理與接口技術(shù)匯編程序-刪除字符串開頭和結(jié)尾的空格 [打印本頁]

作者: 2253318110    時間: 2023-5-21 11:29
標題: 微機原理與接口技術(shù)匯編程序-刪除字符串開頭和結(jié)尾的空格
該程序?qū)崿F(xiàn)了將一個字符串開頭和結(jié)尾的任意多個空格去除然后顯示字符串內(nèi)容。
可以根據(jù)需要輸入不同的字符串,程序?qū)⒆詣幼R別并刪除開頭和結(jié)尾的空格。
適合本科階段學習微機原理與接口技術(shù)的同學練習參考。


  1. ;Remove spaces at the beginning and end of a string

  2. DATA SEGMENT
  3.     string DB ' a string for testing. ',0
  4.     length DW $-string    ;length of the string
  5. DATA ENDS

  6. CODE SEGMENT
  7.     ASSUME CS:CODE, DS:DATA
  8. START:
  9.     MOV AX, DATA
  10.     MOV DS, AX
  11.    
  12.     LEA SI, string
  13.    
  14. BEGIN:    ;Set beginning pointer of the new string
  15.     CMP [SI], ' '    ;Find spaces, skip them
  16.     JNE NEXT
  17.     INC SI
  18.     JMP BEGIN

  19. NEXT:   ;Shift DI to the end of the initial string
  20.     LEA DI, string
  21.     MOV AX, length    ;Load length of the string to AX
  22.     ADD DI, AX      
  23.     SUB DI, 2       ;Move DI to the end of the string
  24.    
  25. THIRD:   ;Shift DI to the end of the new string
  26.     CMP [DI], ' '
  27.     JNE LAST
  28.     DEC DI
  29.     JMP THIRD
  30.    
  31. LAST:   ;display the new string on screen
  32.     INC DI
  33.     MOV byte ptr[DI], '0'   ;string ends with 0
  34.     INC DI
  35.     MOV byte ptr[DI], '


  36.    
  37.     MOV AH, 09H
  38.     MOV DX, SI      ;Set offset adress
  39.     INT 21H
  40.    
  41. CODE ENDS
  42. END START
復制代碼







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1