2006年10月4日 反转字符串

来源:互联网 发布:js实现div闪烁效果 编辑:程序博客网 时间:2024/04/27 22:27
 2006年10月4日 星期3 天气晴   Last Modify: Oct 4 2006
==================================================================================
 终于解决了那个恼人的内存存储数据问题,试验的内容见52.asm,45.asm带来的麻烦也顺利的解决了,真的是拨开迷雾看到了结果,真是不容易。
 到了晚上又做了一个课后题,自己动手做真是没有简单的事情,只是反转一下字符串,已经有拷贝字符串的例子了,但是还是费了不少的功夫,因为字符串最后的那个0,内存中大量的0使我没有考虑到反转以后0的问题,考虑了以后发现问题的复杂程度提升了一个等级……经过分析,终于补全了0,虽然和没补时结果一样……具体的过程见47.asm

**some content deleted from internet**

   Rev 1.0  Creat Document  oct 4 2006

47.asm


;Created by DarkBlue Oct 4, 2006  Rev 1.0
Title Copying a String and invert the string
.386
.model flat, stdcall
option casemap :none

include windows.inc
include kernel32.inc
include masm32.inc
Include irvine32.inc

includelib kernel32.lib
includelib masm32.lib
IncludeLib irvine32.lib

.data
 source BYTE "This is the source string, it will be copied.",0
 target BYTE sizeof source dup(0),0
 comment @
 注意此处的字符串共有45个字符,占用46个字节2E(结尾的那个0)
 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   <--本行为其前面的内存空间的数据
 54 68 69 73 20 69 73 20 74 68 65 20 73 6F 75 72
 63 65 20 73 74 72 69 6E 67 2C 20 69 74 20 77 69
 6C 6C 20 62 65 20 63 6F 70 69 65 64 2E 00
 
 最后说明一点原书未指名的一点,那就是他定义的Target比source要大一个字节,
 因为Target要容纳source的所有数据,包括结尾的那个0,所以要大一点,
 毕竟是个字符串和数据结构的习惯,不知道我说的对不对……
 @
.data?
 buffer BYTE MAX_PATH dup(?)

.code
 main PROC
  
  ;copy string
  mov esi,0    ;index register
  mov ecx, sizeof source ;loop counter
  call DumpRegs
  L1:
   mov al,source[esi] ;get a character from source
   mov target[esi],al ;store it in the target
   inc esi    ;move to next character
  loop L1     ;repeat for entire string
  
  ;show the two string
  mov edx, offset source
  call WriteString
  call Crlf
  mov edx, offset target
  call WriteString
  call Crlf
  
  ;Invert the string
  ;specal Note: 下标从0开始,所以最后一个的元素的下标是(个数-1),
  ;而对于字符串数组来说最后一个字符是0(前面自己定义的),也就是
  ;说我们的反转需要-2,循环(元素个数-1)次,然后补上最后的那个0
  mov esi,0
  mov ecx, (sizeof source)-1 ;在长度上去掉最后一个0
  call DumpRegs
  L2:
   mov al,source[ecx-1] ;在实际中指向队尾
   mov target[esi],al
   inc esi
  loop L2
  
  mov target[esi],0   ;在指向最后一个元素后esi又加了1,故此处esi已指在结尾该添0处
  ;dump Memory
  mov esi, offset target
  mov ecx, lengthof target
  mov ebx, type target
  call DumpMem
  call Crlf

  ;show the inverted string
  mov edx, offset target
  call WriteString
 
  ;暂停显示,回车键关闭
  invoke StdIn,addr buffer,sizeof buffer
  invoke ExitProcess,0 ;如果不写入此语句,将会因非法退出被关闭
  
 main ENDP
END main

原创粉丝点击