该程序实现了将一个字符串开头和结尾的任意多个空格去除然后显示字符串内容。
可以根据需要输入不同的字符串,程序将自动识别并删除开头和结尾的空格。
适合本科阶段学习微机原理与接口技术的同学练习参考。
image.png

;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
  • 复制代码