汇编程序设计入门
1. 求无符号数组BUF中的最大值存放于MAX中。(必做)
data segment buf db 1, 9, 23, 7, 98, 32 n = $-buf max db 0 data ends code segment assume cs:code, ds:data start: mov ax, data mov ds, ax mov cx, n-1 mov si, offset buf mov al, [si] lop: inc si cmp al, [si] jae next mov al, [si] next: loop lop mov max, al mov ax, 4c00H int 21H code ends end start
View Code
2. 求有符号数组BUF中的最小值存放于MIN中。(必做)
data segment buf db -124, -25, 123, -92, 3, 47 n = $-buf ;$表示该行的偏移地址 min db 0 data ends code segment assume cs:code, ds:data start: mov ax, data mov ds, ax ;dec buf ;mov bl, [buf+4] mov cx, n-1 mov si, offset buf mov al, [si] again: inc si cmp al, [si] jle next mov al, [si] next: loop again mov min, al mov ax, 4c00H int 21H code ends end start
View Code
3. 统计数组BUF中正数、负数、0的个数顺序存放在变量NUM中。(必做)
data segment buf db 1, 0, 123, 9, 2, 7, -9, -4, -23, 0 n = $-buf num db 3 dup(0) data ends code segment assume cs:code, ds:data start: mov ax, data mov ds, ax mov cx, n mov si, offset buf again: cmp byte ptr [si], 0 jl a1 jz a2 inc num jmp a3 a1: inc num+1 jmp a3 a2: inc num+2 a3: inc si loop again mov ah, 4cH int 21H code ends end start
View Code
4. 从键盘输入一个正整数,判断是否是回文数,是则输出“YES”,否则输出“NO”。例如1551、131是回文数。(选做)
;判断回文数 data segment maxSize db 30H size db 0 buf db 30H dup(0) s1 db 0aH, 'YES$' s2 db 0aH, 'NO$' data ends code segment assume cs:code, ds:data start: mov ax, data mov ds, ax mov dx, offset maxSize mov ah, 0aH int 21H mov cl, size dec cl mov di, cx sar cx, 1 mov bx, offset buf mov si, 0 again: mov al, [bx+si] cmp al, [bx+di] jne b2 inc si dec di loop again mov dx, offset s1 jmp print b2: mov dx, offset s2 print: mov ah, 09H int 21H mov ah, 4cH int 21H code ends end start ;Debug ;mov al, [bx+si] ;mov ah, [bx+di] ;cmp al, ah ;Question ;以下均使用7654321测试 ;变量名是存放数组所在存储单元的地址 ;1.mov cl, size-1 则cl为30H # ;2.cmp al, [bx+size-1]则al为37H # ;3.mov ax, [bx+6-si]当si通过inc变为1后,再执行ax变为0DH ;4.size dw 0 | mov cx, size则cx为37 07H #因为规定只保留了一个字节存储输入字符数 ;5.mov al, [bx+si+[size]-1] 则al为37H ;6.mov di, word ptr size不可取 #因为size下面一块高地址是未知的 ;Implement ;基址加变址寻址: bx为数组的基址,si/di表示首地址/末地址偏移量 ;或者直接si/di指向首/末,个人认为不如第一种好 ;问题6可用下列方法解决,可用于最下方的实现,对原来的进行替换(不可取!!!原因见问题4) ;mov [size+1], 0 ;mov cx, size ;以下为不用loop的实现: ; mov cl, size ; dec cl ; mov di, cx ; sar cx, 1 ; mov bx, offset buf ; mov si, 0 ; again: cmp si, di ; jae b1 ;//循环出口 ; mov al, [bx+si] ; cmp al, [bx+di] ; jne b2 ; inc si ; dec di ; jmp again ; ; b1: mov dx, offset s1 ; jmp print ; b2: mov dx, offset s2 ; print: mov ah, 09H ; int 21H
View Code
5. 求出所有的水仙花数并输出。(选做)
;水仙花数 data segment num db 1, 0, 0 res db 10 udp(0) data ends code segment assume ds:data, cs:code main proc start: mov ax, data mov ds, ax mov cx, 152 mov di, offset res again1:cmp cx, 160 ja print mov dx, cx call apart call sum cmp bx, cx jne next mov [di], bx inc di next: inc cx jmp again1 print: mov ah, 4cH int 21H main endp apart proc near mov ax, cx mov bl, 100 div bl mov [num], al mov al, ah mov ah, 0 mov bl, 10 div bl mov [num+1], al mov [num+2], ah ret apart endp sum proc near mov bx, 0 mov cx, 3 mov si, offset num again: mov al, [si] mul [si] mul [si] add bx, ax inc si loop again mov cx, dx ret sum endp code ends end start ;Problem ;1.cs不用进行赋值 ;2.子程序写在外面会调用失败并且不停的返回start ;Implement ;因为mul会对ax进行覆写,所以需要求出三位的信息后再求立方和 ;需知道调用的原理,才不会出错
View Code
Reference:
https://blog.csdn.net/jia970426/article/details/85108548
https://blog.csdn.net/yang_tang/article/details/89915674
https://zhidao.baidu.com/question/412110450.html
https://blog.csdn.net/algzjh/article/details/73649546
https://blog.csdn.net/qq_40627648/article/details/84063124
https://blog.csdn.net/baidu_33836580/article/details/50579075
https://blog.csdn.net/carlyll/article/details/50437786
https://zhidao.baidu.com/question/195597064172959525.html
https://blog.csdn.net/sxhelijian/article/details/70365534
http://www.cppblog.com/luqingfei/archive/2010/08/04/122170.html
https://www.cnblogs.com/AbeDay/p/5026848.html
https://zhidao.baidu.com/question/1733497446123593347.html