16位汇编写的一个杨辉三角
杨辉三角在C语言里很常见 下面是C代码和效果图
程序代码:
#include<stdio.h> #define M 12 int main(void) { int a[M][M], i, j; printf("\n\n\n"); for (i = 0; i < M; i++) { printf(" "); for (j = 0; j <= i; j++) { if (i == j || j == 0) a[i][j] = 1; else a[i][j] = a[i - 1][j] + a[i - 1][j - 1]; printf("%-4d", a[i][j]); if (i == j) printf("\n"); } } return 0; }---
现在 汇编来实现这个目的
不过我做这个有点限制 就是限制在12行 即最大数能到百位 要搞更多行可以对程序做出适当的修改。
上代码和效果图, 有兴趣的看看, 那里可以改进下。
=====
程序代码:
;#Mode=DOS ;MASMPlus 单文件代码模板 - 纯 DOS 程序 ;-------------------------------------------------------------------- ;-------------------------------------------------------------------- ; program name: Yang Hui triangle ; producer: yrjd ; program function: Show the Yang Hui triangle as Left angle ; produce data: 2012-10-5 ;-------------------------------------------------------------------- assume cs:code, ds:data, ss:stack stack segment db 128 dup(?) stack ends data segment prompt db '12 lines of Left angle Yang Hui triangle: ', 0dh, 0ah, '$' endStr db 0dh, 0ah, 'Press any key to continue', '$' last db 20 dup (0) next db 20 dup (0) data ends code segment start: mov ax, stack mov ss, ax mov sp, 128 mov ax, data mov ds, ax lea dx, prompt mov ah, 09h int 21h mov dh, 4 mov dl, 8 mov ax, 160 mul dh mov bx, ax mov ax, 2 mul dl add bx, ax mov ax, 0b800h mov es, ax mov cx, 11 mov bp, 1 mov last[0], 1 mov es:[bx + 6], 31h mov es:[bx + 6 + 1], 2 add bx, 160 Set: push cx mov dx, 1 mov next[0], 1 mov es:[bx + 6], 31h mov es:[bx + 6 + 1], 2 dis: cmp dx, bp je SetOne mov di, dx mov al, BYTE ptr last[di] dec di add al, last[di] inc di mov next[di], al mov al, next[di] call TurnToBCD inc dx jmp dis SetOne: mov si, dx mov next[si], 1 mov al, next[si] call TurnToBCD push di mov cx, dx mov di, 1 SetOK: mov ah, BYTE ptr next[di] mov BYTE ptr last[di], ah inc di loop SetOK pop di pop cx inc bp add bx, 160 loop Set mov ah, 2 mov bh, 0 mov dh, 17 mov dl, 0 int 10h lea dx, endStr mov ah, 09h int 21h mov ah, 01h int 21h mov ah, 4ch int 21h TurnToBCD: push bx push cx push dx push si push di push ax push ax mov ax, dx mov dl, 8 mul dl mov di, ax pop ax mov ah, 0 mov cl, 10 div cl add ah, 30h mov dl, ah mov ah, 0 mov cl, 10 div cl add ah, 30h mov dh, ah mov ah, 0 mov cl, 10 div cl add ah, 30h mov ch, ah cmp ch, 30h jz ok1 mov es:[bx + di + 2], ch mov es:[bx + di + 3], 2 ok1: cmp dh, 30h jz ok2 mov es:[bx + di + 4], dh mov es:[bx + di + 5], 2 ok2: mov es:[bx + di + 6], dl mov es:[bx + di + 7], 2 OK: pop ax pop di pop si pop dx pop cx pop bx ret code ends end start----
对贴上去的排版无语 以后有时间修正下 稍微加点注释。。。
眼冒金星了 睡觉去。