汇编语言(六)编程练习题

1.试写一汇编语言程序,要求从键盘接收一个4位的十六进制数,并在终端上显示与它等值的二进制数

data segment
     infor1 db 0ah,0dh,"Please Input a Four-digit Hexadecimal Number!:('Esc' to exit)$"
     infor2 db 0ah,0dh,"Your Input Is Iilegal!$"
     infor3 db 0ah,0dh,"Hex To Binary is:$"
     everbit db 0,0,0,0
data ends
code segment
     assume cs:code,ds:data

start:
	print macro str
		push ax
		push dx
		mov dx,offset str
       		mov ah,09h
      		int 21h
		pop dx
		pop ax
	endm
	
	prind macro ch
		push ax
		push dx
		mov dl,ch
		mov ah,02h
		int 21h
		pop dx
		pop ax
	endm

	digita macro num		
		local cf,nc,ouput1
		push ax
		push cx
		push dx

		mov al,num
		mov cx,4
		shl al,cl
	cf:	
		shl al,1	
                jc ouput1
		prind '0'
	nc:	loop cf	
		
		pop dx
		pop cx
		pop ax

                jmp re2
	ouput1: 
		prind '1'
    		jmp nc
	endm

;;;;;;;;;begin;;;;;;;;;;;


        mov ax,data
        mov ds,ax   
	mov bx,offset everbit
		
input:		
	print infor1   

	mov ah,01h
        int 21h
	mov [bx],al
	
	cmp al,27
	jz exit

	mov bx,offset everbit
	mov cx,3
	mov di,0
save:	
	inc di
	mov ah,01h
        int 21h
	mov [bx+di],al
	loop save
	
	mov di,0
	mov cx,4
        print infor3

read:
	mov al,[bx+di]
	
	cmp al,byte ptr '0'
        jb error
	cmp al,byte ptr 'f'
	ja error
	cmp al,byte ptr '9'
        jbe digit
	cmp al,byte ptr 'a'
        jae letter
	cmp al,byte ptr 'A'
        jb error
	cmp al,byte ptr 'F'
	ja error
	jmp capital

re:
        digita al

re2:        inc di
            loop read

	jmp input

exit: 	
	mov ah,4ch
        int 21h

error:
        print infor2
        jmp input
digit:
	sub al,30h
        jmp re
letter:
        sub al,57h
        jmp re
capital:
        sub al,37h
        jmp re

code ends
     end start

2.从键盘输入一系列字符(以回车符结束),并按字母、数字、其他字符分类计数,最后显示出这三类的计数情况。【目前实现的功能是可输入0~9数量内的字母、数字或其他字符(即字母、数字或其他字符的最大输入量为9),因为读如字符以其ASCII码存储在内存单元里,所以显示统计数字时需要转换,0~9间好转换(0——30h,1——31h,2——32h ... 9——39h)只需要减去或加上30h即可正确显示,而如果是两位数则需要进制的处理,这里以后再改吧,先这样。】

assume cs:code,ds:data

data segment
        infor1 db 0dh,0ah,"Please Input a string:$"
        str    db 100 dup('$')
        count  db 0,0,0
        infor2 db 0dh,0ah,"Digital char: $"
        infor3 db 0dh,0ah,"Letter char: $"
        infor4 db 0dh,0ah,"Other char: $"

data ends

code segment
        prinst macro addre
                push dx
                push ax

                mov dx,offset addre
                mov ah,09h
                int 21h

                pop ax
                pop dx
        endm
        princh macro ch
                push dx
                push ax

                mov dl,ch
                add dl,30h
                mov ah,2
                int 21h
                pop ax
                pop dx
        endm

start:
        mov ax,data
        mov ds,ax

        prinst infor1

        mov dx,offset str
        mov ah,10
        int 21h

        mov si,2
        mov bx,offset str
        mov di,offset count
check:
        cmp [bx+si],byte ptr 0dh
        jz  exit
        cmp [bx+si],byte ptr '0'
        jb  other
        cmp [bx+si],byte ptr 'z'
        ja  other
        cmp [bx+si],byte ptr '9'
        jbe  digit
        cmp [bx+si],byte ptr 'a'
        jae  letter
        cmp [bx+si],byte ptr 'A'
        jb  other
        cmp [bx+si],byte ptr 'Z'
        ja  other

        inc byte ptr [di+1]
        inc si
        jmp check
        
digit:
      inc byte ptr [di]  
      inc si
      jmp check

letter:
      inc byte ptr [di+1]  
      inc si
      jmp check

other:
      inc byte ptr [di+2]
      inc si
      jmp check

exit:
        mov di,offset count
        prinst infor2
        princh [di]        ;这里需要注意宏调用,刚开始写的 princh byte ptr [di],还以为自己很牛X
                           ;结果传过去的参数一直是1,我也不知道他为毛就是1.但在DEBUG查看他就是1 
                           ;可能对宏指令的理解还不够深刻,所以 画蛇添足,多此一举。把自己坑了很久
                           ;把 byte ptr 传过去是1,word ptr 传过去是2; 如果只输入 princh [n] (n为数字),则传过去也就是n(数字),而不是ds:[n]所指的内存单元中的值
        prinst infor3
        princh [di+1]

        prinst infor4
        princh [di+2]

        mov ah,4ch
        int 21h
code ends
        end start

3.键盘输入两个四位十进制数,相加后将结果输出显示。

data segment
	;db 7,7,7,7,7,7,7,7
	infor1 db 0ah,0dh,"Please Input the First Four-Bits Decimal Number:$"
	infor2 db 0ah,0dh,"Please Input the Second Four-Bits Decimal Number:$"
	infor3 db 0ah,0dh,"The Added Result Is:$"
data ends
 
assume cs:code,ds:data

code segment
start: 
        save macro  
	     local save1,save2
		push ax
		push dx
		push di
		push cx
	
		mov dx,offset infor1
        	mov ah,09h
        	int 21h

		mov di,0
		mov cx,4
	save1:	
		mov ah,01h
        	int 21h
		sub al,30h
		mov [di],al
		inc di
                loop save1

		mov dx,offset infor2
        	mov ah,09h
        	int 21h

		mov di,4
		mov cx,4
	save2:	
		mov ah,01h
        	int 21h
		sub al,30h
		mov [di],al
		inc di
                loop save2
		
		pop cx
		pop di
		pop dx
		pop ax
	endm
	
	princh macro ch  
                push dx  
                push ax  
  
                mov dl,ch  
                add dl,30h  
                mov ah,2  
                int 21h  
                pop ax  
                pop dx  
        endm  

	func macro 
	     local a,b,c,d
		push ax
		push bx

		mov ah,ds:[2]
		mov al,ds:[3]
		mov bh,ds:[6]
		mov bl,ds:[7]
		
	    	add ax,bx
		aaa
	;	or  ax,ax	;清除标志位
		

	    	mov ds:[3],al
		mov al,ah
		
		aaa		
		jnc  a
		inc si		;个位向十位进位
		
	    a:		
		mov ds:[2],al
		


		mov ah,ds:[0]
		mov al,ds:[1]
		mov bh,ds:[4]
		mov bl,ds:[5]
		
		cmp si,1	;来自十位向百位的进位
		jnz  b
		inc ax

	    b:	add ax,bx
		cmp al,10h
		jb  e
		add al,6
		
	    e:	aaa	
		jnc  c
		inc si
		
	    c:	mov ds:[1],al
		mov al,ah

		cmp al,10h
		jb  f
		add al,6

	    f:  aaa	
		jnc  d
		inc si
		
	    d:	mov ds:[0],al

		pop bx
		pop ax
	endm

;;;;;;;;;;;;;;;;;;;;;;;Begin;;;;;;;;;;;;;;;;;;;;;;;;;

	mov ax, data
	mov ds, ax
	mov si,0
	
        save
	func

	mov dx,offset infor3
        mov ah,09h
        int 21h

	cmp si,3
	jnz d
	princh 1
d:	princh ds:[0]	
	princh ds:[1]	
	princh ds:[2]	
	princh ds:[3]	
	

	mov ah,4ch
	int 21h
code ends
	end start

猜你喜欢

转载自blog.csdn.net/qq_40818798/article/details/80529175