Jozo
Jozo

Reputation: 115

Assembly code can't handle negative integers

My assembly code consists of two parts, one called A.s and another called B.s.

A.s is my 'main' where I stream output to the terminal.

B.s can not handle negative integers, and also displays the 'Welcome msg' twice.

Here is the code;

B.s code

some code above this-----------------
LoopNextInt:
    movq    $buffInPtr, %r10
    movq    $buffIn, %r9
    addq    (%r10), %r9
    xorq    %r8, %r8

    addq    (%r9),%r8

    cmpq    $63, (%r9)
    je  inImage

    cmpq    $' ', (%r9)
    je  retNeg

    cmpq    $'-', (%r9)
    je  negSignFound

    cmpb    $'0', (%r9)
    jl  retNeg

    cmpb    $'9', (%r9)
    jg  retNeg

    movzbq  (%r9),%r11

    imulq   $10, %rax
    subq    $'0', %r11  #asci -> dec tal
    addq    %r11, %rax
    //addq  $1, (%r10)
    incq    buffInPtr
    //addq  $1, %r9
    incq    buffIn

    jmp     LoopNextInt

some code below this-----------------
then comes this ---------------     
fillOutBuffWithChars:
    movq    $buffOutPtr,%r9
    cmpq    $63, (%r9)  
    je      outImage                
    movzbq  (%rdi), %r8             
    movb    %r8b, (%r10)                
    cmpq    $0, (%rdi)              
    je      zeroInStringFoundz

    addq    $1,%rdi
    addq    $1,%r10
    //addq  $1,%r9
    incq    buffOutPtr
    jmp     fillOutBuffWithChars

zeroInStringFoundz: 
    ret

A test run from the program;

Start. Enter 5 numbers, both negative and pos numbers.
Start. Enter 5 numbers, both negative and pos numbers.
5 1 3 6 -5        
5+1+3+6+0=15
5
125
End
Made by Jozo

I noticed, if I use a sentence that is smaller, like, " Enter 5 numbers ", it will not print it double.

EDIT; Removed unnecessary code so new users can see the problem more easily.

Upvotes: 0

Views: 393

Answers (1)

Jester
Jester

Reputation: 58762

The cmpq $0, (%rdi) should be cmpb $0, (%rdi) or simply cmp $0, %r8b since the byte is already loaded anyway. Given that you compare a quadword instead of a byte, it will not find the end of the string which then causes outImage to be invoked from inside putText and so the text is printed already. But then you call outImage in main again, hence the doubled output.

You have the same problem in LoopNextInt, where all your quadword comparisons should be byte sized. As it is, negSignFound is never reached which in turn means revInt is never reached either.

PS: Learn to use a debugger and comment your code.

Upvotes: 2

Related Questions