0

I am trying to create a C program and ASM program to add and subtract two 64 bit numbers. My addition is correct. However, i am having trouble getting the correct output for subtraction. My subtraction function can be seen in my ASM code under subLoop.

Could someone point me in the right direction for subtracting two 64 bit numbers in ASM?

Please note that i am a beginner with C programming, ASM programming, and stackoverflow. I'm very sorry if i didn't format something correctly on this post or in my code

Here is my C Program which is correct:

#include <stdio.h> 
#include <stdlib.h> 

//C prototype 
extern void stats(int[], int, int *, int *);  


int main() 
{
    int lst[] = {0xa, 0x0000000000000005};
    int len = 2;
    int sum;
    int difference;
    stats(lst, len, &sum, &difference);

    printf("Sum = %x \n", sum);
    printf("Difference = %x\n", difference);
    return 0; 
}

Here is my ASM Program which is half correct (My sumLoop is good, but my subLoop is not):

;------------------------
section .data
;------------------------
section .text
;------------------------

;Call:
;    stats(lst, len, &sum, &difference);

global stats

stats:    
push r12     
;-------------------
mov r11, 0
mov r12d, 0

sumLoop:
    mov eax, dword[rdi+r11*4]   ;get lst[i]
    add r12d, eax               ;update sum
    inc r11                     ;i++
    cmp r11, rsi
    jb sumLoop

    mov dword [rdx], r12d       ;return sum
;---------------------
subLoop:
    mov eax, dword[rdi+r11*4]
    sub r12d, eax
    inc r11
    cmp r11, rsi
    jb subLoop

    mov dword [rcx], eax
;----------------------
pop r12
ret
zx485
  • 26,827
  • 28
  • 51
  • 55
Boar
  • 9
  • 3
    In C, `int` is usually 32 bits, `long` is 64 bits. – Barmar Apr 27 '22 at 22:26
  • You do not seem to be trying to achieve 64 bit addition/subtraction. Rather, you want arbitrary precision. Otherwise you could just use a single `add`/`sub`. Even your addition function is wrong anyway, since it does not propagate carry. – Jester Apr 27 '22 at 22:26
  • 6
    My suggestion would be to code it in `c` and look at what the compiler emits for that as a guide. – 500 - Internal Server Error Apr 27 '22 at 22:28
  • 1
    What instruction set / architecture are you using? x86_64 (64-bit) is probably the most common these days if you are using a PC, but I bet there are some people reading this question from an ARM machine or an i686 (32-bit) machine. What OS, compiler, and assembler are you using? Could you show what commands you are running to test this and what the output is? (I suppose the correct output would be 15 for the sum and 5 for the difference?) – David Grayson Apr 27 '22 at 22:32
  • 1
    If you step through the code in a debugger, you may notice something interesting about `r12` and `r11` when you reach `subLoop` for the first time. – Raymond Chen Apr 27 '22 at 22:40
  • @RaymondChen i unfortunately cannot do that from my knowledge. I'm using vscode and i have no idea how to do that. Could you explain what is interesting about r12 and r11? – Boar Apr 28 '22 at 00:40
  • 5
    A debugger is one of the essential tools of programming. Go find a debugger and learn to use it. – Raymond Chen Apr 28 '22 at 00:59
  • @DavidGrayson the assembly posted above is definitely x86-64 – phuclv Apr 28 '22 at 01:25
  • @Boar [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/995714), [Debugging in VS Code](https://code.visualstudio.com/Docs/editor/debugging) (of course you need to set up first, otherwise use VS instead, no setup is required) – phuclv Apr 28 '22 at 01:26
  • 1
    Might want to change the label `subLoop` to `diffLoop` Os some other name to avoid confusion if you develop a stuffy nose. – Caleb Apr 28 '22 at 02:31
  • @thebusybee I don't think the goal is extended precision arithmetic. Just calculating the sum and difference of two values. – Raymond Chen Apr 28 '22 at 13:47
  • @thebusybee The output is a single `int`, not an array of ints, so even if you kept track of carries, you have nowhere to put them. – Raymond Chen Apr 28 '22 at 16:00
  • @RaymondChen Uh, yes, I read too fast. Sorry for the noise. :-( – the busybee Apr 28 '22 at 19:39

0 Answers0