• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

Assembler Language Help

Status
Not open for further replies.
Level 17
Joined
Feb 11, 2011
Messages
1,860
Hi guys,

I hope someone here knows assembler language. I have to do this assignment for university which has to display the first few terms of the Fibonacci Sequence (3 to 14 terms). I can't seem to get it right. It displays the correct number of terms, but their values are really strange.

Code:
.model small
.stack
.data

    msg_num  db 10, 13, "Enter how many Fibonacci numbers must be displayed: $"
    msg_oper db 10, 13, "Enter which operator must be used: $"
    msg_inv  db 10, 10, "Number must be between 03 and 14 (inclusive)!"
    crlf     db 10, 13, "$"

    char1 db ?
    char2 db ?
    num   db ?
    cur   db '2'
    fib1  db '0'
    fib2  db '1'
    ans   db ?

.code

start:

    ;****************************************;
    ; Load data.                            *;
    ;****************************************;

    mov ax, @data
    mov ds, ax
    
    ;****************************************;
    ; Prompts for reading in number.        *;
    ;****************************************;

    @GET_INPUT:
    
        mov ah, 09
        mov dx, offset msg_num
        int 21h

        mov ah, 01
        int 21h
        mov char1, al

        mov ah, 01
        int 21h
        mov char2, al

        cmp char1, '0'
        je @SINGLE_DIGIT
        cmp char1, '1'
        je @DOUBLE_DIGIT
        jmp @INVALID

    @SINGLE_DIGIT:
    
        cmp char2, '3'
        jl @INVALID
        mov al, char2
        mov num, al
        jmp @PROCEED
        
    @DOUBLE_DIGIT:
    
        cmp char2, '4'
        jg @INVALID
        add char2, 10
        mov al, char2
        mov num, al
        jmp @PROCEED
        
    @INVALID:
    
        mov ah, 09
        mov dx, offset msg_inv
        int 21h
        jmp @GET_INPUT
        
    @PROCEED:
        
        ;=== Prints first 2 Fibonacci numbers ===;
        
        mov ah, 09
        mov dx, offset crlf
        int 21h
        
        mov ah, 02
        mov dl, fib1
        int 21h
        
        mov ah, 09
        mov dx, offset crlf
        int 21h
        
        mov ah, 02
        mov dl, fib2
        int 21h
        
        @WHILE:
            
            mov al, num
            cmp cur, al
            jl @ADD
            jmp @EXIT
            
            @ADD:
                
                ; ans = fib1 + fib2
                mov    al, fib2
                add al, fib1
                mov ans, al
                
                ; fib1 = fib2
                mov al, fib2
                mov fib1, al
                
                ; fib2 = ans
                mov al, ans
                mov fib2, al
                
                add cur, 1
                
                ; new line:
                mov ah, 09
                mov dx, offset crlf
                int 21h
                
                ; print answer:
                mov ah, 02
                sub ans, 48
                mov dl, ans
                int 21h
                
                jmp @WHILE


    ;****************************************;
    ; Exits the program.                    *;
    ;****************************************;

    @EXIT:
        
        mov ax, 4c00h
        int 21h

end start
Please note that I am very new to this and we don't get taught anything - we just have to figure it out ourselves.
Hopefully someone can help!

Thanks,

Mr_Bean
 
Level 6
Joined
Sep 20, 2012
Messages
179
Could you write an example of the output you get? It would be simplier to find a mistake then.
 
Level 8
Joined
Aug 13, 2009
Messages
466
This takes me back to when I was doing an assembly assignment (used MIPS, with the somewhat cryptic emulator SPIM...). Jesus assembly is a royal pain in the ass, thank god for modern programming languages.
 
Status
Not open for further replies.
Top