assembly,output,sequence , How do I break the string in output into 2 parts in this assembly code


How do I break the string in output into 2 parts in this assembly code

Question:

Tag: assembly,output,sequence

So I want to break the break "The nth term in Fibonacci Series is " string into two:

"The " and "th term in Fibonacci Series is ", print the first string, then print the value of n (say using WriteInt), then print the second string.

But I can't seem to get the right output, any tips on how to break them into two?

This is my output now (let's say I entered 4 as input):

Display Fibonacci Series
Enter number of fibonacci series terms: 4
Fibonacci Series is
0
1
1
2
Sum of Fibnoacci Series = 4
Press any key to continue... 

How can I change it to this input that I want:

Display Fibonacci Series
Enter the number in fibonacci sequence number: 4
The 4th sequence in Fibonacci Series is
2
Press any key to continue... 

Also right now the formula in this code is fibonacci, how do I change it to new formula like this;

f(n-2)/2 + f(n-1) * 2

This is my code:

INCLUDE Irvine32.inc

.DATA
; insert variables here
  dPrevious DWORD 0   
    dCurrent  DWORD 1   
    dNext DWORD 1     
    dNumberOfTerms DWORD 0
    dSum DWORD 0
    msg1 BYTE "Enter the Fibonacci sequence number : ",0 ; A null-terminated string
    msg2a BYTE "The ", 0
    msg2b BYTE "th term in Fibonacci Series is ", 0
    msg3 BYTE "th term in Fibonacci Series is ", WriteString
    msg4 BYTE "Error: Fibonacci Series must have at least 2 numbers",0



 .CODE
; insert all executable statements here
main PROC
    mov edx, OFFSET msgFibonacciSeries  ; "Display Fibonacci Series and Sum of Fibonacci Series in Assembly Language "
    call WriteString                    ; Display a null-terminated string.
    call Crlf                           ; Writes an end-of-line sequence to the console window.

    mov edx, OFFSET msg1      ; "Enter Number of Fibonacci Series Terms : "
    call WriteString          ; Display a null-terminated string.
    call ReadInt              ; Input a 32-bit signed decimal integer
                              ; from the keyboard and save in EAX.
  mov dNumberOfTerms, eax

    mov ebx,dWords   
    cmp ebx,1                ; compare EBX and 1
  jle Error                ; jump if Less than or equal  (<=)

    mov edx, OFFSET msg2a     ; "Fibonacci Series is "
    call WriteString 
    mov edx, OFFSET msg2b        
    call Crlf                ; Writes an end-of-line sequence to the console window.

    mov eax,dPrevious
    call WriteDec            ; Display an unsigned 32-bit integer value
                             ; in EAX in decimal format. 

    call Crlf



    mov ecx,dNumberOfTerms    ; ECX  initialize with Number Of Terms
    sub ecx,2                 ; Exclude First Two Terms

    cmp ecx,0                 ; compare EBX and 0
  je showSum                ; jump if equal  (=)

       L1:
    mov ebx,dPrevious
        add ebx,dCurrent
     mov dNext,ebx
    mov eax,DNext
     cmp ecx,1
         jne ffff
           ;add dSum,eax
    ffff:

       loop L1
   showSum:     

    jmp next
  Error:
    mov edx, OFFSET msg4     ; "Error: Fibonacci Series Contain at least 2 number of terms"
    call WriteString
     cmp eax,1         
    call Crlf
  next:                
call WaitMsg    ; Displays a message and waits for a key to be pressed.
exit            ; The exit statement (indirectly) calls a predefined 
                ; MS-Windows function that halts the program.
main ENDP  ; The ENDP directive marks the end of the main procedure.
END main       ; The END directive marks the last line of the program to be assembled.

Answer:

To split your message, make it into two data items:

msg2a BYTE "The ", 0
msg2b BYTE "th term in Fibonacci Series is ", 0

Then in the code:

mov edx, OFFSET msg2a     ; "Fibonacci Series is "
call WriteString 
; ...
; Code here to write out the value of `n`
; ...
mov edx, OFFSET msg2b        
call Crlf

This won't be so elegant if n is 1 or 2 because it will say, 1th or 2th. I will leave it as an exercise to fix that if you want to. You can break your message up further for that if needed. For example:

msg2a BYTE "The ", 0
msg2b BYTE "st", 0
msg2c BYTE "nd", 0
msg2d BYTE " term in Fibonacci Series is ", 0

And adjust according to the value of n.

As far as modifying the formula from f(n-1) + f(n-2) to f(n-1)*2 + f(n-2)/2, locate where where f(n-1) and f(n-2) are determined and use the fact that *2 is the same as a single bit shift left, and /2 is the same as a single bit shift right. It's left as another exercise. If you look things up, it should be quite simple. :)


Related:


How is a file loaded in the FAT file system?


filesystems,fat32,assembly
I am developing a bootloader in x86 Assembly and I have a question about the FAT file system. Which steps are necessary to find and load the file?...

MinGW's ld cannot perform PE operations on non PE output file


gcc,assembly,mingw,nasm,osdev
I know there are some other similar questions about this out there, be it StackOverflow or not. I've researched a lot for this, and still didn't find a single solution. I'm doing an operative system as a side project. I've been doing all in Assembly, but now I wanna join...

Python algorithm error when trying to find the next largest value


python,algorithm,file,output
I've written an algorithm that scans through a file of "ID's" and compares that value with the value of an integer i (I've converted the integer to a string for comparison, and i've trimmed the "\n" prefix from the line). The algorithm compares these values for each line in the...

Error: Junk at EOL, first unrecognised character is '('


assembly,gas,quake
I am trying to compile Quake 1 (QW) from source code and have run into the following problem when compiling client/d_copy.s : Error: Junk at EOL, first unrecognised character is '('. The file in question is here (please excuse the syntax highlighting, pastebin only supports NASM). I am attempting to...

Make the input wait for mouse or keyboard - Assembly Language


assembly,input,keyboard,mouse,simultaneous
I fixed my program, but now the loop seems to be stuck. Whenever I press u or d, it is stuck, but the mouse part works :) ; You may customize this and other start-up templates; ; The location of this template is c:\emu8086\inc\0_com_template.txt org 100h CLEAR MACRO MOV AX,0600H...

OutPut the values of a program


java,output
Here is have a program that will output the following: 3 6 3 6 7 The program is this: public class test3 { static int x=3; public static void main(String args[]){ test3 t = new test3(); System.out.println(x); int x=6; System.out.println(x); System.out.println(t.x); t.xupdate (x); System.out.println(x); System.out.println(t.x); } public void xupdate(int y){...

MASM SEG operator


assembly,masm
Let's assume we are writing assembly code using MASM 6.1 / 16 bit / huge data model, and we have a variable (label) named MY_VAR, a segment named MY_SEG and a segment group named MY_GROUP. Let's assume MY_VAR is placed in MY_SEG, and MY_SEG belongs to MY_GROUP. Then, what is...

NASM assembled bootloader memory issue


assembly,nasm,bootloader
I am writing a bootloader with nasm. At the moment it is designed to output a welcome string, then record keystrokes while displaying them, printing the stored keystrokes upon finding the enter key, and finally halting. bits 16 org 0x7C00 start: jmp main bgetkey: pusha mov ax, 0 mov ah,...

What does DX + 2 mean in mov ah,9 int 21h?


assembly
mov dx, offset buffer mov ah, 0ah int 21h jmp print buffer db 10,?, 10 dup(' ') print: xor bx, bx mov bl, buffer[1] mov buffer[bx+2], '$' mov dx, offset buffer + 2 mov ah, 9 int 21h I know buffer[bx+2] stands for '$', but offset buffer +2 in mov...

Python outputs a .txt file, which's format differs depending on the text editor I use to open it


python,text,output
So I have some python code taht outputs some data to a .txt file like this: f3 = codecs.open(r'C:\Users\dimrizo\Desktop\PythonData\GTFS\routes.txt','w+',"UTF-8") f3.write('route_id,agency_id,route_short_name,route_long_name,route_desc,route_type,route_url,route_color,route_text_color\n') f3.write('blah,blah,blah,blah,blah,blah,blah,blah,blah\n') Problem is that if I open the produced file with the simple windows text editor the text is not properly formatted. The "\n" don't even count. If I open...

Print string using INT 0x10 in bootsector


assembly,x86,fasm
I want to create printl function that allow me to print string in the ax register. I am in 16-bit real mode and I can not find any way to print a message. I using int 0x10 to print a single letter. I try pass argument (string to print) in...

storing array from user and accessing it


arrays,assembly,input,user,mips
I have to create an int array based on user input, sum the integers, and output the sum and the array itself. My code calculates the sum correctly, but I cannot output the list. It only prints out the last number and zeros and then gets stuck in an infinite...

subl causing Floating point exception


assembly,x86
I am creating a compiler which should compile a fictional language into asm x86 code. When compiling this piece of code (fictional code): int x; int f(int n) { write n; } int main() { x = 1; f(x); } write is equal to just print in console. The desired...

NASM: copying a pointer from a register to a buffer in .data


linux,assembly,nasm,x86-64
I am new to asm. I am trying to copy a pointer from a register to a .data variable using NASM, on linux 64-bit. Concider this program: section .data ptr: dq 0 section .text global _start _start: mov [ptr], rsp mov rax, 60 mov rdi, 0 syscall Here I try...

Range of Addresses for a Conditional Branch Instruction in MIPS


assembly,mips
What is the range of addresses for a conditional branch instruction in MIPS with respect to X, where X contains the address of the branch instruction? Assume the current PC value is 0x10000000. What is the range of addresses for jump (j) and the jump and link (jal) in- structions...

Asm x86 segmentation fault in reading from file


assembly,x86,segmentation-fault,mmap
I'm receiving segmentation fault in 5th line of loop2. This is the part of my code which is reading values from mmapped file byte by byte. Could you please tell me what I'm doing wrong? .global _start _start: mov $2,%rax mov 16(%rsp),%rdi mov $02,%rsi syscall cmp $0,%rax jl exit 1:...

How can I access the individual elements of an array in a loop?


assembly,mips
I need to print the cells of an array, I have an array which contains the word "HELLO_WORLD", I manage to print an index by its own but I can't manage to print all the cells one by one, here is the code : loop: la $t0, hexdigits # address...

How do I check assembly output of Java code?


java,assembly
I found this question that answered it for C++: How do you get assembler output from C/C++ source in gcc?

How to get return value from a forked / spawned process in Ruby?


ruby,process,output,fork,spawn
My simple test program: pid = Process.spawn("sleep 10;date") How can I place the output (eg stdout) of the "date" command in a variable when it is available? I don't want to use a file for the data exchange....

GCC emits vastly different code using “-march=native” on similar architectures


c,gcc,assembly,sse,avx
I'm working on writing an OpenCL benchmark in C. Currently, it measures the fused multiply-accumulate performance of both a CL device, and the system's processor using C code. The results are then cross checked for accuracy. I wrote the native code to take advantage of GCC's auto vectorizer, and it...

Macro to push arguments onto stack


visual-c++,assembly,macros
I've been working on a fun little project to mess around with how functions are called and I need a macro to push arguments as it'll be quite time consuming to push the arguments manually for every instance of this obfuscated call. This is my code so far: #define pushargs(...)...

Open Watcom Inline assembly SEG and OFFSET operators


c,assembly,memory-address,watcom
Today, I have learned that the SEG operator in MASM by default returns the address of the GROUP and not the address of the SEGMENT of the expression in question, and that there are options and methods to override that. Since I am currently doing a complex project in Open...

References - Why do the following two programs produce different output?


c++,reference,static,output
I recently read about references in C++. I am aware of basic properties of references but I am still not able to figure out why following two programs produce different output. #include<iostream> using namespace std; int &fun() { static int x = 10; return x; } int main() { fun()...

ARM assembly cannot use immediate values and ADDS/ADCS together


gcc,assembly,arm,instructions
I am currently trying to speed up some of my C functions on a Cortex-M0 (Freescale KL25Z) using assembly. I get a problem with this minimal test program: @.syntax unified .cpu cortex-m0 .text .global test .code 16 test: mov r0, #0 adds r0, r0, #1 bx lr When I try...

How does this instruction look in memory?


assembly,x86
I am having trouble figuring out how this instruction looks in memory for a x86 processor. mov $0x3c,%eax Can someone help me figure it out? For example an easy one is: xor %edi,%edi ---> 0x31 0xFF...

Perform integer division using multiplication


assembly,optimization,bit-manipulation,division,multiplication
Looking at x86 assembly produced by a compiler, I noticed that (unsigned) integer divisions are sometimes implemented as integer multiplications. These optimizations seem to follow the form value / n => (value * ((0xFFFFFFFF / n) + 1)) / 0x100000000 For example, performing a division by 9: 12345678 / 9...

How to represent mips instruction as it's hex representation


assembly,mips
I'm given a MIPS instruction: top: lw $t1, ($t0) beq $s0, $0, end jal func add $s0, $s0, $t0 addi $s0, $s0, -1 j top bne $s0, $0, top end: func: sll $v0, $t1, 4 jr $ra and am told to convert each line to the "instruction in hex." What...

Why Masm32 only give 1 to 100 result for add and subtraction operation and beyond that I got wrong answer?


assembly,masm32
I am new to assembly language. I make this code just now and run it with no errors so far, except It will only give a result from 1 - 100, here's my code. This is a simple math operation which is addition and subtraction. I tried analyzing the code...

Why can't I use compiler intrinsics in an asm block?


delphi,assembly
Is this a compiler bug? program Project44; {$APPTYPE CONSOLE} uses System.SysUtils; function Test: integer; asm xor eax,eax add eax,SizeOf(NativeInt); end; begin WriteLn(Test); //Typically outputs 31 or 49 {$ifdef CPUX86} WriteLn('should be 4'); {$else} {$ifdef CPUX64} WriteLn('should be 8'); {$endif}{$endif} ReadLn end. This program outputs all kinds of things, except for...

Is this an overflow, or maybe more keyboard data?


assembly,nasm,bootloader
I am writing a bootloader, and it's functionality is basically limited to printing a string, then copying keyboard characters to the screen as they are typed. While writing the routines that read and write the key, I noticed my print routine was not detecting the null terminator in offset (plus)...

Counter not working after jumps - assembly language


loops,assembly,counter,increment
For some reason, when i switch to mouse input switch back to keyboard input for my program, increasing and decreasing the counter has no effect. It works perfectly in the first loop where we input characters. Here is the program guys, any advice? look at whatspeed jump for reference after...

correct output for this fork concept in C


c,output,fork,atoi
So for some reason I can't get the output of this code in C, so I can only ask a few conceptual question on this code below: What does int N = atoi(argv[i]); do? is it just define integer N as an array? In the for loop, what does if...

Range of immediate values in ARMv8 A64 assembly


gcc,assembly,arm64
My understanding is that immediate parameters in ARMv8 A64 assembly can be 12 bits long. If that is the case, why does this line of assembly code: AND X12, X10, 0xFEF Produce this error (when compiled with gcc) Error: immediate out of range at operand 3 -- `AND X12, X10,...

nasm: jump when input is NULL


c,assembly,nasm,libc
I am recoding puts using nasm (64bit), and when puts receives NULL as argument it prints (null). I'm trying to recreate that behaviour, except I can't get the code to jump to the part where it prints (null). Instead it just prints nothing here's my code: global _my_puts section .text...

LC3 assembly-how to count string length


string,assembly,lc3
I am trying to create a program at LC3 assembly that counts the length of a string in the following way: All data is already stored somewhere in memory. There is a variable in which the address of the first element of the string is stored. (I apologize in advance...

assembly function with C segfault


c,assembly,x86,sse,fpu
I am trying to make assembly function that uses SSE and FPU for parallel calculations. Unfortunately I am receiving segmentation fault(core dumped) error(while debugging it doesn't show in assembly function). I also cannot step out from assembly function. Gdb shows: Warning: Cannot insert breakpoint 0. Cannot access memory at address...

Reserve bytes in stack: x86 Assembly (64 bit)


assembly,x86-64,cpu-registers
pushq %rbp movq %rsp, %rbp subq $32, %rsp I have big question regarding explanation of "$32" in third instruction. The information from search and blogs specifies that in above third instruction we are reserving certain "bytes" of space for stack. From doc, %rsp is 64 bit register and %esp is...

Need help in writing this multiplication code


c,formatting,output,multiplication,spaces
I need to show process of multiplication in output.for ex:- inputs are two numbers(say 325 and 4405) the output would be:- 325 *4405 ----- 1625 0 1300 1300 ------- 1431625 NOTE:-number of spaces should be minimum. I don't need the exact code. Just tell me which property or way I...

Visual Studios building and debugging .cpp file without main()


c++,assembly,visual-studio-2013
Professor just gave us a block of c++ code in order to learn about the debug windows in Visual Studio, however after creating a blank win32 console project and then dragging in the .cpp file I get the missing .exe error message when attempting to debug. I've looked everywhere and...

Understanding NASM Macro


assembly,macros,ffmpeg,nasm
I've come around this macro in a assembly source file and I just can't figure out how it's working. So first I come around this function (hevc_deblock.h): cglobal hevc_v_loop_filter_chroma_8, 3, 5, 7, pix, stride, tc, pix0, r3stride sub pixq, 2 lea r3strideq, [3*strideq] mov pix0q, pixq add pixq, r3strideq TRANSPOSE4x8B_LOAD...

Why don't I get any output?


c++,class,methods,output
I am trying to write my first OOP code in C++, but for some reason I am not getting any output. I am trying to create a class that contain a method getSquare() that accept an int n and returns the number squared. Can anyone tell me where I am...

How is shellcode generated from C? - With code example


python,c,gcc,assembly,shellcode
I started taking an online course regarding software security. In one of the sections, I was instructed to execute a hidden C function using a buffer overflow. I got to thinking: what would happen if I could pass machine instruction directly to a stack unsafe executable? What I have been...

NASM: Makefile for library


assembly,makefile,nasm
I'm having trouble building a makefile for a library in nasm, since it requires that you run nasm with one input file at a time. I have tried with the %.o : %.s thing but I'm probably doing it incorrectly since it's not working. Here is what I have: NAME...

x86 jmp asterisk %eax


assembly,x86,jmp
In the paper for Google Native Client, the authors define a nacljmp as these two instructions: and %eax, 0xffffffe0 // Clears the 5 least significant bits in %eax. jmp *%eax First, I can see they clear the 5 least signficiant bits to make the instruction 32 bit aligned before it...

Execute multiple command lines with output


c#,cmd,output
I want to perform some command lines to display the result after each input. Process p = new Process(); ProcessStartInfo info = new ProcessStartInfo(); info.FileName = "cmd.exe"; info.RedirectStandardInput = true; info.UseShellExecute = false; p.StartInfo = info; p.Start(); using (StreamWriter sw = p.StandardInput) { if (sw.BaseStream.CanWrite) { sw.WriteLine("ftp"); //output sw.WriteLine("open ftp.server.com");...

Wrong answer from DIV assembly


assembly,x86
I have a part of my code mov di,3 mov cx,16 looop: xor dx,dx shl bx,1 adc dx,dx cmp cx,16 je cx16 (it's will dec cx and jump loop) push dx dec cx cmp cx,0 je cx0 mov ax,cx div di cmp dx,0 jne looop when cx = 3 i...

How to jump to an address saved in a register in intel assembly? [duplicate]


assembly,jmp
This question already has an answer here: conditional jump to register 1 answer say I calculated an address of a label and stored it in eax register, how can I JMP(specifically JE) to eax? jmp eax compiles, but I didn't check if it works. je eax doesn't compile(invalid combination...

LC3 assembly-unable to print the right character


assembly,encryption,lc3
I have been trying to make a program that decrypts a text that is being read from a file under the following condiditons: The adress of the first element of the file is 5001.Each letter is encrypted twice:The first time with Ceasar encryption and the second time with XOR encryption.The...

Declaring Variables in the .data Versus on the Stack - ASM


assembly
I'm trying to understand in which cases you would want to declare variables in the .data section of my assembly program and using it for the procedures needed, over instantiating local variables on the stack and vice versa. Is it just that declaring variables in the .data section is only...

How to make my output show only whats needed with my cheque generator program for c


c,input,output
I have made a program that can output whatever numeral is inputted in to it but it will convert it to words instead of just numerals for example if you input "1234.56" it will convert it to "One Thousand Two Hundred Thirty Four Dollars and ... 56 Cents". The cents...