汇编语言程序:处理字符

大小写转换问题

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

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

大写二进制小写二进制
A01000001a01100001
B01000010b01100010
  • 大写+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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值