SoFunction
Updated on 2025-03-04

Assembly language to implement printing Yang Hui triangle

Assembly language to implement printing Yang Hui triangle

Updated: February 5, 2020 09:45:52 Author: didididu1515
This article mainly introduces the assembly language to print the Yang Hui triangle. The example code is introduced in this article in detail, which has certain reference learning value for everyone's study or work. Friends who need it, please learn with the editor below.

Calculate the first n (n<=10) row of Yang Hui triangle and display it on the screen. Required calculation and display
Implemented in subroutine form. Its display format is:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

CODE SEGMENT 
 ASSUME CS:CODE,DS:CODE 
 org 100h
 START: jmp begin
message db 13,10,9,'Input N(N&lt;=10): $'
error db 13,10,9,'Data out of range!$'
 begin:
  push cs
  pop ds
  mov dx,offset message
  mov ah,9
  int 21h

  call shur
  cmp bp,10
  jbe goon
  mov dx,offset error
  mov ah,9
  int 21h
  jmp exit
 goon:
  mov ax,0e0dh
  int 10h
  mov al,0ah
  int 10h
 push bp
  call yhsj
 exit:
  mov ah,0
  int 16h
  mov ah,4ch
  int 21h
 
 shur proc
 push cx
 push bx
 xor bp,bp
 mov bx,10
 mov cx,2
 input:
 mov ah,0; keyboard input data
 int 16h
 cmp al,0dh; end input with Enter
 jz ok
 cmp al,'0' ;Only 0~9 is allowed
 jb input
 cmp al,'9'
ja input
mov ah,0eh ;Show valid input
int 10h
sub al,30h ;changeASCIIforHEX
cbw ;字节扩展for字
xchg ax,bp
mul bx ;expand10Double
add bp,ax ;Add one
loop input
ok:nop ;The numerical result isBPmiddle
;Recover the registers used
pop bx
pop cx
ret
shur endp


; Functions that output Yang Hui triangle,Accept parameters on a stackN
; OutputNYang Hui Triangle
yhsj:
 mov bp, sp
 mov ax, [bp+2]  ; saveNarriveax
 shr ax, 1  ; N = N / 2
 push ax
 mov ax, [bp+2]  ; saveNarriveax
 push ax
 call C; C(N, N/2)获取最后一行middle间的那个值,That is, the maximum value
 call getdigit   ; Calculate the length of this maximum value,like252Then return3
 mov cx, ax ; saveMaximum lengtharrivecx,Used for post-event format
 xor di, di ; Outer loop countdi,外层cycleOutput每一行
 jmp cp1
up1:
 inc di; renewdi
cp1:
 cmp di, [bp+2]  ; Test cycle conditions,cycleNSecond-rate
 jg done1
 mov ax, [bp+2]  ; the following3Sentence calculates the number of spaces before line = (N-i)*cl,clIt is the maximum length
 sub ax, di
 mul cl
 call showspace  ; Output行前空格
 xor si, si ; 内存cycle计数si,内层cycleOutput一行middle的每Number of
 jmp cp2
up2:
 inc si; renewdi
cp2:
 cmp si, di ; Test cycle conditions,cyclediSecond-rate
 jg done2
 push si
 push di
 call C; Get the line's locationsiNumber of combinations of positions,CallC(di, si)
 push ax   ; save该组合数
 call show  ; Output该数
 mov ax, cx ;┒the following3句Output数字间间隔空格,Number of = N - 1
 sub ax, 1  ;┃
 call showspace  ;┚
 pop ax;┒
 call getdigit   ;┃Get the length of the combination
 mov bx, ax ;┃
 mov ax, cx ;┃
 sub ax, bx ;┃Calculate the number of spaces to be filled = Maximum length - The length of this number + 1
 add ax, 1  ;┃本来应该先填充再Output数字间空格,顺序反过来是for了左对齐
 call showspace  ;┚The above brackets2The segments are in the normal order in turn
 jmp up2   ; renew内层cycle
done2: ; 内层cycle结束
 mov ah, 2  ; the following5Sentences implement line breaks
 mov dl, 13
 int 21h
 mov dl, 10
 int 21h
 jmp up1   ; renew外层cycle
done1: ; 外层cycle结束
 ret 2 ; Free the stack space used by function parameters

; Recursive function to find combination numbers,Accept the stack2Parametersn, m(n &gt; m)
; returnC(n, m),Right nownselectm的Number of
; The algorithm is:
; { C(n, m) = 1 (n &lt; m or m = 0)
; { C(n, m) = C(n-1, m-1) + C(n-1, m) (n &gt; m)
; Right now某位置组合数等于上一行左右两数之和
C:
 push bp
 mov bp, sp
 sub sp, 2  ; Reserve a storage location
 mov bx, [bp+6]  ; savemarrivebx
 cmp bx, [bp+4]  ; like果m &gt; n return1
 jz L1
 cmp bx, 0  ; like果m = 0 return1
 jz L1
 mov ax, [bp+4]  ; savenarriveax
 dec ax; ax = ax - 1
 dec bx; bx = bx - 1
 push bx
 push ax
 call C; return上一行左边的那Number of
 mov [bp-2], ax  ; save左肩膀上的数
 mov ax, [bp+4]  ; the following5Same sentence,return上一行右肩膀上的数
 dec ax
 push [bp+6]
 push ax
 call C
 add ax, [bp-2]  ; Add the numbers on the left shoulder to get the combined number
 jmp L2
L1:
 mov ax, 1
L2: 
 mov sp, bp
 pop bp
 ret 4 ; axreturn组合数

; Recursively10进制Outputax
; The method is very simple,Just find the remainder,Thenax = ax / 10
; ax = 0Exit when,开始逆序Output求出的各位余数
show:
 mov bx, 10
 cmp ax, 0
 jz ok1
 div bl
 push ax
 and ax, 00ffh
 call show
 pop dx
 mov dl, dh
 or dl, 30h
 mov ah, 2
 int 21h
ok1:
 ret

; 获取一Number of的长度,axforparameter,like果ax = 252Then return3
; ax里是return值
getdigit:
 mov bx, 10
 xor dx, dx
next:
 cmp ax, 0
 jle ok2
 div bl
 and ax, 0ffh
 inc dx
 jmp next
ok2:
 mov ax, dx
 ret

; OutputaxA space,parameterax,无return值
showspace:
 mov bx, ax
 mov ah, 2
 mov dl, ' '
nexts:
 cmp bx, 0
 jle dones
 int 21h
 dec bx
 jmp nexts
dones:
 ret

 CODE ENDS 
 END START

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.

  • compilation
  • Yang Hui Triangle

Related Articles

  • Assembly instructions-detailed description of status registers, cmp, test, jz and other instructions

    This article mainly introduces the detailed description of assembly instructions - status registers, cmp, test, jz and other instructions. Friends who need it can refer to it
    2020-01-01
  • Specific usage of DIV instructions in assembly language

    This article mainly introduces the specific usage of DIV instructions in assembly language
    2024-03-03
  • Implementation code for array allocation and pointer in assembly

    This article mainly introduces the implementation code of array allocation and pointer in assembly. The example code is introduced in this article in detail, which has certain reference learning value for everyone's study or work. Friends who need it, please learn with the editor below.
    2020-01-01
  • Assembly language series implements simple mathematical operations

    This article mainly introduces a detailed explanation of the ideas of implementing simple mathematical operations in the assembly language series. This article lists the codes of two arithmetic operations. The design ideas are introduced to you in a very detailed way, which has certain reference value for your study or work. Friends who need it can refer to it.
    2021-11-11
  • asm basics—the in/out instruction of assembly instructions

    This article mainly introduces the basics of asm - the in/out instruction of assembly instructions. The example code is introduced in the article in detail, which has a certain reference learning value for everyone's study or work. Friends who need it, please learn with the editor below.
    2020-01-01
  • Specific methods to implement if else loop function calls using assembly language

    This article mainly introduces the specific methods of using assembly language to implement if else loop function calls. The article introduces the example code in detail, which has certain reference learning value for everyone's learning or work. If you need it, please learn with the editor below.
    2020-01-01
  • Assembly Function call implementation

    This article mainly introduces the implementation of assembly function calls. The article introduces the example code in detail, which has certain reference learning value for everyone's learning or work. Friends who need it, please learn with the editor below.
    2020-02-02
  • Debug command detailed explanation tutorial for assembly language

    This article mainly introduces a tutorial on the assembly language Debug command. The article provides a comprehensive explanation of the Debug commands in the article. Friends in need can learn from them and learn them together. I hope it will be helpful.
    2021-11-11
  • Detailed explanation of the role of assembly language learning assume

    This article mainly introduces the detailed explanation of the role of assembly language learning assume. Friends in need can refer to it for reference. I hope it can be helpful. I wish you more progress and get a promotion as soon as possible.
    2021-11-11
  • Summary of using debug commands in assembly language

    Debug is a debugging tool for real-mode (8086 method) programs provided by DOS and Windows. This article mainly introduces the summary of the use of debug commands in assembly language. It has certain reference value. If you are interested, you can learn about it.
    2024-03-03

Latest Comments