4-1
解法一----堆栈
include Irvine32.inc
.data
buf word 12AFh
mas byte 4 dup(?)
.code
main proc
push offset buf
push offset mas
call decbin
exit
main endp
decbin proc
push ebp
mov ebp,esp
push esi
push eax
push ebx
push ecx
push edx
xor esi,esi
mov ecx,4
mov ebx,[ebp+12] ;取出buf偏移地址
mov edi,[ebp+8] ;取出mas首地址
again:
mov dx,word ptr [ebx]
rol dx,4
mov word ptr[ebx],dx
and dl ,0fh ;0fh=(1111)B,取dl低四位
cmp dl,0ah ;<0ah,该位为数字,dl=dl+30h
jb num
add dl,7 ;字母的Ascii码为dl=dl+7h
num:
add dl,30h
mov byte ptr [edi+esi],dl ;存入mas
movzx eax,dl
call writeint
call crlf
inc esi
loop again
pop edx
pop ecx
pop ebx
pop eax
pop esi
ret
decbin endp
end main
解法二----参数传递(个人喜欢这种写法)
include Irvine32.inc
.data
buf dword 1234h ;此处改为DWORD,,是为了下面mas的赋值
mas byte 4 dup(?)
.code
main proc
call d1
exit
main endp
d1 proc
xor esi,esi
mov ecx,4
again:
mov dx,word ptr buf
rol dx,4
mov word ptr buf,dx
and dl,0fh
cmp dl,0ah
jb num
add dx,7
num:
add dx,30h
mov mas[esi],byte ptr dl ;用byte ptr dx会出错
mov eax,dword ptr mas[esi]
call writeint
call crlf
inc esi
loop again
ret
d1 endp
end main
tips:刚开始一直不清楚转换ascii码为什么存在加7和加30的说法,后来去csdn上搜了一下ASCII表,才发现十六进制表示的ASCII码等于30+所转换的数/7+所要转换的字符。自己也用Dev c++亲测了一下,确实如此。
4-2
解法一----参数传递(个人推荐)
include Irvine32.inc
.data
n equ 5
str1 byte n dup (?)
.code
main proc
mov ecx,n
lea edx,str1 ;此处注意与offset的区别,lea是取地址
call readstring ;输入字符串
call ts ;大小写转换
lea edx,str1
call writestring ;输出字符串
exit
main endp
ts proc
mov ecx,n
xor esi,esi
again:
mov al,str1[esi]
cmp al,'a' ;凡是小于'a'的都转换为小写字母,避免了二次判断
jae next
add al,32
mov str1[esi],al
next:
inc esi
call writeint
loop again
call crlf
ret
ts endp
end main
解法二----堆栈参数(行数太多,不够简洁,个人不推荐)
include irvine32.inc
n=50
.data
string db n dup(?)
len dd n
.code
main proc
push offset len
push offset string
call rdstring ;调用输入字符串函数
push offset len
push offset string
call dwstring ;输出转换后的字符串的ASCII码
call crlf
lea edx,string ;输出转换后的字符串
call writestring
exit
main endp
rdstring proc
push ebp
mov ebp,esp
push eax
push ecx
push edx
push esi
mov esi,dword ptr [ebp+12]
mov ecx,dword ptr [esi]
mov edx,dword ptr [ebp+8]
call readstring
mov dword ptr [esi],eax
pop esi
pop edx
pop ecx
pop eax
pop ebp
ret 8
rdstring endp
dwstring proc
push ebp
mov ebp,esp
push eax
push ecx
push edx
push esi
mov esi,dword ptr [ebp+12]
mov ecx,dword ptr [esi]
mov edx,dword ptr [ebp+8]
mov esi,0
again:
cmp byte ptr [edx+esi],41h ;A
jb next
cmp byte ptr [edx+esi],5ah ;Z
ja next
add byte ptr [edx+esi],20h
next:
movzx eax, byte ptr [edx+esi]
call writeint
inc esi
loop again
pop esi
pop edx
pop ecx
pop eax
pop ebp
ret 8
dwstring endp
end main