THIS IS A MICROPROCESSOR ASSEMBLY LANGUAGUE. I USE TEXTPAD 7 TO TYPE CODE AND DEBUG CODE BY DOSBOX
Factorial(N) = n*(n-1)*(n-2)*...*2*1
(a) Write a procedure to calculate F(N). Pass the offsets of N and F. The procedure returns nothing. Create a stackframe before the procedure call, and destroy it after the call.
Hi,
The assembly language code to calculate factorial of a number is shown below.
.model small
.data
x dw 5 -- Here we are calculating the factorial of 5.
dw stands for define word.
.code
mov ax,@data -
mov ds,ax - we cannot move data into the ds directly.
we have to use ax register to move data to the ds(data
segment)
mov cx,x - initializing counter
register(cx) to the value x i.e., 5
mov ax,x -initializing ax to the value x i.e., 5
call fact - calling fact funtion
fact:
l: dec x - decrementing the x value by 1.
mul x - multiplies ax with x and stores the result in ax.
dec cx - decrementing counter register(cx) by1 .
jnz l - we have to repeat this procedure till counter register(cx)
becomes 0. When it is '0' it need to exit the loop.
ret - the ax value will be return.
mov ah,4ch
int 21h
end
b)macro to calculate factorial of a number:
FACTORIAL MACRO F ----DEFINING FACTORIAL MACRO
L: ----LABEL TO LOOP BACK TO THE DESIRED LOCATION
MUL F ----MULTIPLY THE NUMBER IN THE F REGISTER
DEC F --- DECREMENT THE NUMBER
JNZ L --- JUMP IF NOT ZERO TO L BACK
ENDM ---END OF MACRO
DATA SEGMENT ---INITIALIZE DATA SEGMENT
X DW 06H ---INITIALIZE X ANY DATA
RESULT DW ? ---INITIALIZE RESULT
DATA ENDS ---END OF DATA SEGMENT
CODE SEGMENT ---INITIALIZE CODE SEGMENT
START: ---START THE CODE
ASSUME CS:CODE , DS:DATA --ASSUMPTION OF CODE AND DATA
MOV AX,DATA ---MOV DATA INTO ACCUMULATOR AX
MOV DS,AX ---MOV DATA FROM ACCUMULATOR INTO DATA SEGMENT
REGISTER
MOV CX,X ---MOV X INTO THE CODE SEGMENT CX REGISTER WE MOVE THE
NUMBER INTO CS REGISTER BECAUSE CS REGISTER ACT AS COUNT
MOV AX,0001H ---MOV CONSTANT 0001H INTO AX
FACTORIAL X ---CALL THE MACRO FUNCTION BY SPECIFYING NAME OF
PARAMETERS (OBJECT) TO PASS
MOV RESULT,AX ----MOVE THE RESULT FROM AX INTO RESULT
VARIABLE
MOV AH,4CH -----;END OF THE CODE
INT 21H ----- ;END OF THE CODE
CODE ENDS ----END OF CODE SEGMENT
END START ----END OF START
Hope this answers your question. If you liked the answer please give thumbs up and if you any questions please comment. I will try to solve them. Thank you.
THIS IS A MICROPROCESSOR ASSEMBLY LANGUAGUE. I USE TEXTPAD 7 TO TYPE CODE AND DEBUG CODE...
Write this code using x86 assembly language using the irvine32 library Create a procedure that fills an array of doublewords with N random integers, making sure the values fall within the range j...k, inclusive. When calling the procedure, pass a pointer to the array that will hold the data, pass N, and pass the values of j and k. Preserve all register values between calls to the procedure. Write a test program that calls the procedure twice, using different values...
ASSEMBLY LANGUAGE (Mars MIPS) Starting code: Factorial: #Factorial Recursive function subu $sp, $sp, 4 sw $ra, 4($sp) # save the return address on stack beqz $a0, terminate # test for termination subu $sp, $sp, 4 # do not terminate yet sw $a0, 4($sp) # save the parameter sub $a0, $a0, 1 # will call with a smaller argument jal Factorial # after the termination condition is reached these lines # will be executed lw $t0, 4($sp) # the argument I...
Write a method named factorial that accepts an integer n as a parameter and returns the factorial of n, or n!. A factorial of an integer is defined as the product of all integers from 1 through that integer inclusive. For example, the call of factorial(4) should return 1 2 3 4, or 24. The factorial of 0 and 1 are defined to be 1. You may assume that the value passed is non-negative and that its factorial can fit...
Write an assembly code to do the following noting that the used microprocessor is MC68k: The 7-Segment Display: Use a 7-segment display and a set of current lining resistors (330 to 470 Ohm) in a common anode configuration with the output port of the VIA to display the value of the switch (For example, if the switch is set to [ON OFF OFF ON] then display9) The show goes like this: on power up, you have to show a count...
1. Assume that you are given values in eax, ebx, ecx. Write an assembly code that does the following: eax = (ecx + edx ) - (eax + ebx) 2. Write a piece of code that copies the number inside al to ch. Example: Assume that Initially eax = 0x15DBCB19. At the end of your code ecx = 0x00001900. Your code must be as efficient as possible. 3. You are given eax = 0x5. Write one line of code in...
INTEL 80x86 ASSEMBLY LANGUAGE CODE Write a windows32 assembly language program that utilizes a recursive procedure. The main (_MainProc) procedure should: accept, from the user, a positive integer. Guard against non-positive integers being entered using a loop. call the sumseries sub-procedure using the cdecl protocol, receive the results of the sub-procedure, and display the results. The sumseries sub-procedure should: recursively find the sum of the series: 1*2 + 2*3 + 3*4 + ... + i*(i+1) (This is an iterative definition....
NOTE: explain the lines in comments for better understanding Write an assembly program (for x86 processors - Irvine) that has two procedures, a main procedure and a procedure called Fib. The fib procedure is to uses a loop to calculate and printout the first N Fibonacci numbers. Fibonacci sequence is described by the following formula: Fib(1) = 1, Fib(2) = 1, Fib(n) = Fib(n – 1) + Fib(n – 2). The value of N is to be communicated to this...
PLEASE USE VERY BASIC REGISTERS AND CODE TO DO THE FOLLOWING Objectives: -write assembly language programs to: -define a recursive procedure/function and call it. -use syscall operations to display integers and strings on the console window -use syscall operations to read integers from the keyboard. Assignment Description: Implement a MIPS assembly language program that defines "main", and "function1" procedures. The function1 is recursive and should be defined as: function1(n) = (2*n)+9 if n <= 5 =...
Practice Problem [no points]: You should read and understand Fibonacci algorithm, write a code in a high level language of your choice and in Assembly to find a Fibonacci number and check your result. At the end of this assignment a possible solution to this problem is given both in Python and MIPS assembler. Note that it would have been possible to write the Python differently to achieve the same function. . [20 points] Write and debug a MIPS program...
In assembly code, and if you're writing it out please write neatly so I can be able to read it. Thank you. 5. Assume double-word references to memory and that count is in ECX. Give a fragment of assembly code that implements the design structures shown. The code should flow the same as the design: a. Ifcoun> value then end if fa+b- count 0; b. Assume the character check is in AL Then Else end if f(ch 2 'a) and...