How to Add Numbers in Assembly Language without using a loop? - math

I need this program to print out 1 + 3 + 4 + 10 = 18, but I haven't been successful so far. I can make it to print out 18 alone, but this is not what I am asked to do. I am not allowed to use loops. Could anyone help me with that?
INCLUDE Irvine32.inc
.data
y1 DWORD 1
y2 DWORD 3
y3 DWORD 4
y4 DWORD 10
plus byte " + ",0
equal byte " = ",0
.code
main PROC
exit
main ENDP
END main

OK, it took my entire last night to figure out, but this works.
INCLUDE Irvine32.inc ; like import
.data
y1 dword 1
y2 dword 3
y3 dword 4
y4 dword 10
plus byte " + ",0
equal byte " = ",0;
.code
main PROC
mov eax,0
mov edx,offset plus
mov ebx,0
mov eax,y1
call writedec
add ebx,eax
call writestring
mov eax,y2
call writedec
add ebx,eax
call writestring
mov eax,y3
call writedec
add ebx,eax
call writestring
mov eax,y4
call writedec
add ebx,eax
mov edx,offset equal
call writestring
mov eax,ebx
call writedec
exit
main ENDP
end main

Related

Assembly compare to 0

New to assembly.
I'm trying to write an assembly code, that will do the following operation:
x/8 + 32 - y IF (x+y)/2>0
OR
2Y - 60 IF (x+y)/2<=0
While I was able to get the first operation to work ( even though, only when putting even numbers as X and Y, because otherwise I get results like ~+8000 ), I can't manage to make the second operation to work. When inserting in console something like : x= - 6, y=2, it should prind me out -56, but it prints me +30.
What did I do wrong there and how could I correct it?
INCLUDE Irvine32.inc
.data
mes1 byte "X:",0
mes2 byte "Y:",0
mes3 byte "Result:",0
vrx dword 0
vry dword 0
rez dword 0
doi dword 2
vxr dword 0
vyr dword 0
.code
main PROC
mov edx,OFFSET mes1
call WriteString ;
call ReadDec ;
mov vrx,eax ;
mov edx,OFFSET mes2
call WriteString ;
call ReadDec ;
mov vry,eax ;
;
xor eax,eax
mov edx,0
mov eax, vrx ;x
add eax, vry ;x+y
div doi ;(x+y)/2
cmp eax, 0 ; comparing
jb con1 ;
mov eax,vrx ; X/8+32-Y
mov ebx,8
div ebx
add eax,32
sub eax,vry
mov rez,eax
jmp ex ; jump to ex
con1: mov eax,vry ; 2Y-60
mov ebx,2
mul ebx
sub eax,60
mov rez,eax
ex: mov edx,OFFSET mes3
call WriteString ;
call WriteInt ;
call Crlf ;
exit
main ENDP
END main

Fibonacci with recursion steps shown indented / nested

This is the format that I need:
F(3) = F(2) + F(1) =
F(2) = (F1) + F(0) =
F(1) = 1
F(0) = 1
F(2) = 1
F(1) = 1
F(3) = 2
and this is my code, how am I going to do to get the format I want?
Please give me a hint or something that may help, thank you. I just start learning assembly language.
I only know how to show the first line like f()= the answer, but don't know how to show the process.
.data
fib1 BYTE "f(",0
fib2 BYTE ") + f(",0
fib3 BYTE ") = ",0
intVal DWORD ?
main PROC
mov edx, OFFSET fib1 ;show f(intVal)=
call WriteString
mov edx, intVal
call WriteDec
mov edx, OFFSET fib3
call WriteString
mov ecx, intVal-1
push intVal
call fib
add esp, 4
call WriteDec ;show result
call crlf
mov edx, OFFSET msg5 ;show goodbye msg
call WriteString
mov edx, OFFSET username
call WriteString
exit
main ENDP
fib PROC c
add ecx, 1
push ebp
mov ebp, esp
sub esp,4
mov eax, [ebp+8] ;get value
cmp eax,2 ;if ((n=1)or(n=2))
je S4
cmp eax,1
je S4
dec eax ;do fib(n-1)+fib(n-2)
push eax ;fib(n-1)
call fib
mov [ebp-4], eax ;store first result
dec dword ptr [esp] ;(n-1) -> (n-2)
call fib
add esp,4 ;clear
add eax,[ebp-4] ;add result and stored first result
jmp Quit
S4:
mov eax,1 ;start from 1,1
Quit:
mov esp,ebp ;restore esp
pop ebp ;restore ebp
ret
fib ENDP
END main
The code needs to output a "newline" at the end of each line of output, which could be a carriage return (00dh) followed by a linefeed (00ah) or just a linefeed (00ah) depending on the system (I don't know the irvine setup).
The indented lines should be printed from within the fib function, which means you have to save (push/pop stack) any registers that the print functions use.
The fib function needs to print out a variable number of spaces depending on the level of recursion, based on the text, 2 spaces per level of recursion.
The fib function needs to handle an input of 0 and return 0.
Note that the number of recursive calls to the fib function will be 2 * fib(n) - 1, assuming fib checks for fib(0), fib(1), fib(2) (more if it doesn't check for fib(2) ), which would be 5.94 billion calls for fib(47) = 2971215073, the max value for 32 bit unsigned integers. You may want to limit inputs to something like fib(10) = 55 or fib(11) = 89 or fib(12) = 144.

Attempting to implement this recursive Fibonacci sequence gives wrong outputs

I am trying to implement Fibonacci sequence in assembly by using recursion. This is my first time of trying to implement recursion in x86 Assembly.
The code compiles fine but it gives wrong outputs. The output for 1 is 1, output for 2 is 0, output for 3 is 1, output for 4 is 2, output for 5 is 3.
Only output it gives correct when you plug 5 in.
Is there something wrong with the algorithm?
.DATA
n1 DWORD ?
prompt1 BYTE "Please enter the first value", 0
prompt3 BYTE "No negative numbers!",0
string BYTE 40 DUP (?)
resultLbl BYTE "The Fib is: ", 0
fib BYTE 40 DUP (?), 0
.CODE
_MainProc PROC
input prompt1, string, 40
atod string
test eax, eax
js signed
mov n1, eax
jmp procName
signed:
output prompt3, string
jmp end1
procName:
mov eax, n1
push n1
call fib1
add esp,4
dtoa fib, eax
output resultLbl, fib
end1:
mov eax, 0
ret
_MainProc ENDP
Fib1 proc
PUSH EBP ; save previous frame pointer
MOV EBP, ESP ; set current frame pointer
MOV EAX, [EBP+8] ; get argument N
CMP EAX, 1 ; N<=1?
JA Recurse ; no, compute it recursively
MOV ECX, 1 ; yes, Fib(1)--> 1
JMP exit
Recurse:
DEC EAX ; = N-1
MOV EDX, EAX ; = N-1
PUSH EDX ; save N-1
PUSH EAX ; set argument = N-1
CALL Fib1 ; compute Fib(N-1) to ECX
POP EAX ; pop N-1
DEC EAX ; = N-2
PUSH ECX ; save Fib(N-1)
PUSH EAX ; set argument = N-2
CALL Fib1 ; compute Fib(N-2) to ECX
POP EAX ; = Fib(N-1)
ADD ECX, EAX ; = Fib(N-1)+FIB(N-2)
exit:
MOV ESP,EBP ; reset stack to value at function entry
POP EBP ; restore caller's frame pointer
RET
Fib1 endp
END

Volume of cone not returning correct value

My code assembles correctly, however it is not returning the volume of a cone. I tried a few other ways to get it to give me the volume of a cone, and it doesn't give me the correct answer. Is there something I am missing?
Edit: I fixed my code to output a number, however its not giving me the right number.
This is the formula I am using for this code:
V= (22)(r)(r)(h)/21
diviser DWORD 21
height WORD 0
radius BYTE 0 ;*** Declare an unsigned, integer variable for the value
product WORD 0
askRadius BYTE "What is the radius?", 0ah, 0dh, 0 ;*** Declare the prompt and message parts
askHeight BYTE "What is the height? ", 0
volumeOutput BYTE "The volume of the cone is: ", 0
lineBreak BYTE " ",0dh, 0ah, 0
.code
main PROC
mov ebx, diviser ;Initializes the diviser
mov edx,OFFSET askRadius ;Asks for Radius
call WriteString
call ReadDec
mov radius, al ;moves radius into al (8-bit)
mul radius ;Multiplies radius * radius(16-bit)
mov edx,OFFSET askHeight ;asks for Height
call writestring
call ReadDec
mov height, dx ;moves height into AX
mov WORD PTR product, ax ;convert 16-bit into 32-bit
mov WORD PTR product+2, dx ;converts 16-bit into 32-bit
mul eax ;multiplies by 22
mov edx, 22
mul edx
div ebx ;divides by 21
mov edx, OFFSET volumeOutput
call WriteString
call WriteDec
call WaitMsg
exit
main ENDP
END main
Something like this should work better. Note you should choose where you want to widen the calculation to 64 bit.
askRadius BYTE "What is the radius?", 0ah, 0dh, 0 ;*** Declare the prompt and message parts
askHeight BYTE "What is the height? ", 0
volumeOutput BYTE "The volume of the cone is: ", 0
lineBreak BYTE " ",0dh, 0ah, 0
.code
main PROC
mov edx,OFFSET askRadius ;Asks for Radius
call WriteString
call ReadDec
imul eax, eax ; radius * radius
mov ecx, eax ; save for later
mov edx,OFFSET askHeight ;asks for Height
call writestring
call ReadDec
imul eax, eax, 22
mul ecx ;multiplies by radius * radius
mov ecx, 21
div ecx ;divides by 21
mov edx, OFFSET volumeOutput
call WriteString
call WriteDec
call WaitMsg
exit
main ENDP
END main

Recersive proc in x8086 Assembly

;==============================================================================================
; recursive procedure:
; supersum(int x)
; returns 1*2 + 2*3 + 3*4 + ... + i*(i+1)
supersum PROC
push ebp ; start of every procedure
mov ebp, esp
push ebx
; Actual subproc calc here
mov eax, [ebp +8] ; returning eax to the original called value
cmp eax,1
je basecase
dec eax ; (n-1) ; its just a lie...
mov recnum, eax ; saving dec val for call
mul double ; 2(n-1)
mov rhs, eax ; the right hand side is finished
push recnum
call sumseries ; a_(n-1)
add esp, 4
definition:
add eax, rhs ; a_(n-1)+ 2(n-1)
jmp skp
basecase: ; a_1 = 0
mov eax, 0
mov rhs, 2
jmp definition
skp:
pop ebx
pop ebp
ret
supersum ENDP
;============================================================================
The explicit definition of the serries I'm trying to get is 1*2 + 2*3 + 3*4 ... + i(i+1).
I've got the math for it down and I found that the recursive definition for the series is a_n = a_(n-1) + 2(n-1) with a_1 = 0 as a base case. I'm trying to figure out why this code keeps giving me the even series: {2,4,6,8,10 ...} instead of the series I'm trying to calculate

Resources