标题: 微机原理与接口技术汇编程序-删除字符串开头和结尾的空格 [打印本页]

作者: 2253318110    时间: 2023-5-21 11:29
标题: 微机原理与接口技术汇编程序-删除字符串开头和结尾的空格
该程序实现了将一个字符串开头和结尾的任意多个空格去除然后显示字符串内容。
可以根据需要输入不同的字符串,程序将自动识别并删除开头和结尾的空格。
适合本科阶段学习微机原理与接口技术的同学练习参考。


  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.51hei.com/bbs/) Powered by Discuz! X3.1