0

I have a simple program here that is supposed to take two integers from the user and add them together. However, for some reason, I am getting an insanely huge result every time. I was wondering if someone could explain why that is happening.

.file "addition.s"
.section .rodata
message1:
.string "Please enter an integer on the next line:\n"
SC_1:
.string "%li"
message2:
.string "Please enter a second integer on the next line:\n"
SC_2:
.string "%li"
message3:
.string "Your integer + %ld = %ld\n"
.data
x:
.quad 0
y:
.quad 0
.globl main
    .type main, @function
.text
main:
pushq %rbp
movq %rsp, %rbp
movq $message1, %rdi
movq $0, %rax
call printf
movq $x, %rsi
call scanf
movq $message2, %rdi
movq $0, %rax
call printf
movq $y, %rsi
movq $SC_2, %rdi
movq $0, %rax
call scanf
addq %rsi, x   #I also tried addq $y, x which did not work (why?)
movq x, %rsi
movq $message3, %rdi
movq $0, %rax
call printf
movq $0, %rax
leave
ret
.size main, .-main""

This program is code I copied from a program that originally took an integer form the user then added 5 to it. Because of that, there are some things I don't understand about it which I will mention if anyone could possibly provide an explanation:

What is SC_1 and SC_2 doing (I added the second one since I figured it had to do with taking input)/ What is the function of the string "%li"? I guess wrapped up in that question I am also trying to understand how the compiler knows where to store the value scanned in from scanf? Why do I have to have .size main, .-main at the end and what does it do?

disclaimer : I have to use this syntax (and the gcc) because I am learning this in a class in university and this program is me trying to practice and understand how all this works (my teacher is really not very helpful unfortunately)

Paul
  • 53
  • 6
  • SC_1 and 2 are 2 copies of the same format string for scanf. This is just `scanf("%li", &y)` and `&y`, similar to what you'd get from a C compiler (https://godbolt.org/) using `long int` global variables. [Using scanf with x86-64 GAS assembly](https://stackoverflow.com/q/27095286) You have to add the values from memory after scanf stores results there, not add garbage left over in RSI to the values. – Peter Cordes Mar 28 '22 at 01:57
  • *Why do I have to have .size main, .-main at the end* -You don't. *and what does it do?* - creates some metadata that a linker might use if you were making a shared library out of this. `.` is the current position, so `. - main` is `end - start` for the main function's machine-code bytes. – Peter Cordes Mar 28 '22 at 02:00
  • `addq $y, x` should assemble and link if `movq $message2, %rdi` does. Adding the address to `x` is obviously a bug, but so is adding whatever garbage is left in RSI when scanf returns. So not sure what you're asking what that; are you claiming this code works and asking for an explanation? Or why it doesn't? Either way, that should be covered by the linked duplicates. – Peter Cordes Mar 28 '22 at 02:06
  • @PeterCordes Your first two comments make sense to me although I did check out that question you linked and was not really following some of the code in the responses (such as the leaq lines. I really do not understand what those do and would need a breakdown of the f(%rdi) etc). As for your third comment, the code gives me some huge number that is not correct. I have changed the code slightly now and I am able to get the values given my the user to print correctly but their sum total (held in a separate variable 'z') still always prints out some huge number even when I use input like 2 and 5 – Paul Mar 28 '22 at 23:56
  • Re: RIP-relative LEA: that's the standard way, as [How to load address of function or label into register](https://stackoverflow.com/q/57212012) explains. Once you're done, `mov x(%rip), %rdx` ; `mov y(%rip), %rsi` ; `add %rsi, %rdx` should load both scanf results, (with `y` in printf's 2nd arg, where it will look for the first `%ld` conversion in the format string), and add them into RDX, the 3rd arg printf will look for the 2nd `%ld` conversion. A compiler would make asm like that. – Peter Cordes Mar 29 '22 at 02:13

0 Answers0