How to resolve the algorithm Pascal's triangle step by step in the X86 Assembly programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pascal's triangle step by step in the X86 Assembly programming language

Table of Contents

Problem Statement

Pascal's triangle is an arithmetic and geometric figure often associated with the name of Blaise Pascal, but also studied centuries earlier in India, Persia, China and elsewhere. Its first few rows look like this: where each element of each row is either 1 or the sum of the two elements right above it. For example, the next row of the triangle would be: So the triangle now looks like this: Each row   n   (starting with row   0   at the top) shows the coefficients of the binomial expansion of   (x + y)n.

Write a function that prints out the first   n   rows of the triangle   (with   f(1)   yielding the row consisting of only the element 1). This can be done either by summing elements from the previous rows or using a binary coefficient or combination function. Behavior for   n ≤ 0   does not need to be uniform, but should be noted.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pascal's triangle step by step in the X86 Assembly programming language

Source code in the x86 programming language

%include "io.inc"

section .text
global CMAIN
CMAIN:
	mov ebx, 7 ;size
	call mloop
	ret
	
mloop:
	mov edx, 0 ;edx stands for the nth line
looping:
	push ebx
	push edx
	call line
	pop edx
	pop ebx
	inc edx
	cmp edx, ebx
	jl looping
	xor eax, eax
	ret
	
line:
	mov ecx, 0 ;ecx stands for the nth character in each line
	mlp:
		push ecx
                push edx
		call nCk
                pop edx
		pop ecx
		PRINT_DEC 4, eax ;print returned number
                PRINT_STRING " "
		inc ecx
		cmp ecx, edx ;if
		jle mlp
		NEWLINE
		ret
		
nCk:
	;ecx : j
	;edx : i
	mov esi, edx
	call fac ;i!
	push eax ;save i!
	mov esi, ecx
	call fac ;j!
	push eax ;save j!
	mov ebx, edx
	sub ebx, ecx ;(i-j)
	mov esi, ebx
	call fac ;(i-j)!
	pop ebx ;(i-j)! is in eax
	mul ebx ;(i-j)! * j!
	mov ecx, eax
	pop eax ; get i!
	div ecx ; ; last step : i! divided by (i-j)! * j!
	ret
	
fac:
	push ecx
        push edx
	mov eax, 1
	mov ecx, esi
	cmp ecx, 0 ; 0! returns 1
	je facz
	lp:
		mul ecx ;multiplies eax by ecx and then decrements ecx until ecx is 0
		dec ecx
		cmp ecx, 0
		jg lp
		jmp end
	facz:
		mov eax, 1
	end:
                pop edx
		pop ecx
		ret

  

You may also check:How to resolve the algorithm Loops/Nested step by step in the Sather programming language
You may also check:How to resolve the algorithm Number reversal game step by step in the Raku programming language
You may also check:How to resolve the algorithm Horizontal sundial calculations step by step in the Logo programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the MANOOL programming language
You may also check:How to resolve the algorithm Parsing/RPN calculator algorithm step by step in the Common Lisp programming language