汇编语言程序:处理字符

大小写转换问题

  1. 汇编语言程序中,用’…'的方式指明数据事宜字符的形式给出的,编译器把它们转化为相对应的ASCⅡ码。

大写字母的ASCⅡ码值加上20H则为该字母的小写ASCⅡ码值。

大写 二进制 小写 二进制
A 01000001 a 01100001
B 01000010 b 01100010
  • 大写+20H–>小写
  • 小写+20H–>大写

问题:

对datasg中的字符串
第一个字符串:小写字母转换为大写字母
第二个字符串:大写字母转换为小写字母

assume cs: codesg, ds: datasg
datasg segment
	db 'BaSiC'
	db 'iNfOrMaTiOn'
datasg ends
codesg segment
	...
codesg ends
end start

思路

  • 对第一个字符串,若字母是小写,转大写;否则不变;
  • 对第二个字符串,若字母是大写,转小写;否则不变。
    看似要用到分支结构。但实际上只需要用到and/or逻辑指令。

逻辑与指令: and dest, src
逻辑或指令:or dest, src

代码实现

小写字母转大写字母:

mov bx, 0
mov cx, 5
s: mov al, [bx]
and al, 11011111b
mov [bx], al
inc bx
loop s

大写字母转小写字母:

mov bx, 5
moc cx, 11
s0: mov al, [bx]
or al, 00100000b
mov [bx], al
inc bx
loop s0

[bx+idata]的含义

[bx+idata]表示一个内存单元,它的偏移地址为(bx)+idata (bx中的数值加上idata)。
mov ax, [bx+200] / mov ax, [200+bx]的含义

  • 将一个内存单元的内容送入ax
  • 这个内存单元的长度为2字节(字单元),存放一个字
  • 内存单元的段地址在ds种,偏移地址为200加上bx中的数值
    指令mov ax, [bx+200]的其他写法(常用)
  • mov ax, [200+bx}
  • mov ax, 200 [bx]
  • mov ax, [bx].200
assume cs: codesg, ds: datasg
datasg segment
	db 'BaSiC'
	db 'MinIX'
datasg ends
codesg segment
start: mov ax, datasg
	mov ds, ax

	mov bx, 0
	mov cx, 5
s: mov al, [bx]
	and  al, 11011111b
	mov [bx], al

	mov al, [5+bx]
	or al, 00100000b
	mov [5+bx], al
	inc bx
	loop s

	mov ax, 4c00h
	int 21h
codesg ends
end start

SI和DI寄存器

问题

用寄存器SI和DI实现将字符串’welcome to masm!’ 复制到它后面的数据区中

程序定义

assume cs: codesg, ds: datasg
datasg segment
	db 'welcome to masm!'
	db'................'
datasg ends
codesg segment
 ....
codesg ends
end

源数据起始地址:datasg:0
目标数据起始地址:datasg: 16

  • 用ds:si指向要复制的原始字符串
  • 用ds:di指向目的空间
  • 然后用一个循环来完成复制。
assume cs: codesg, ds: datasg
datasg segment
	db 'welcome to masm!'
	db'................'
datasg ends
codesg segment
 ....
codesg ends
codesg segment
start: mov ax, datasg
		mov ds, ax

		mov si, 0
		mov di, 16
		mov cx, 8
	s: mov ax, [si]	;[si]源地址
		mov [di], ax	;[di]目标地址
		add si, 2
		add di ,2
		loop s

		mov ax, 4c00h
		int 21h
codesg ends
end start

猜你喜欢

转载自blog.csdn.net/m0_71290816/article/details/127253421