Hi sinth,
Thanks for joining us! The first mistake might be not giving us enough information...
%include "io.inc"
This could be anything. I'm going to take a wild guess that it's the late Sivarama Dandimudi's include file, and you're probably supposed to link against his "io.obj". However, you appear to be trying to bypass his routines and replace them with calls to the C library. This could be done, but the interface is completely different.
Oh, wait! I'm mistaken. Dr. Dandamudi calls his include file "io.mac" not "io inc", so I'm completely off-base. Maybe you'd better start by telling us what "io.inc" is and where we might find it...
Blundering blindly ahead... regardless of what "io.inc" is, you still need to call the C libraries with the correct "calling convention". In 32-bit code (64-bit code is completely different!), we are expected to push the parameters on the stack, and we are expected to "clean up the stack" or "remove the parameters" after the function returns. Nothing actually gets "removed" - the parameters are still on the stack, but we've moved the stack pointer (esp) above them so the stack space can (and will) be re-used. The "last" or "rightmost" (as we would write them in C) gets pushed first, the "first" or "leftmost" parameter gets pushed last. In the case of _printf or _scanf the "first" (last pushed) parameter needs to be a "format string" - a zero-terminated(!) string including some "tokens" which tell our function what to do. The number of "tokens" tells our function how many more parameters to expect on the stack. We can probably get by with just a format string and one more parameter...
The C functions return the "result" in eax, but in this case it isn't the "result" we're looking for. I think _printf returns the number of bytes printed - it isn't very interesting. _scanf returns the number of items read. It may be worthwhile to check this value to make sure the pesky user entered something valid. For simplicity, we can "assume a well behaved user". If the pesky user enters "garbage" it's going to screw up the rest of our program royally. A "better" program would check!
So where were we?
; probably don't really need the include file
global _main
extern _printf
extern _scanf
section .data
prompt1: db 'enter the 1st no.', 0
prompt2: db 'enter the 2nd no.', 0
out_msg1: db 'you entered', 0
out_msg2: db 'the result is', 0
; some format strings...
string_fmt db '%s', 0
int_fmt db '%d', 0
section .bss
input1: resd 1
input2: resd 1
section .text
;global _main
_main:
mov ebp, esp; for correct debugging
enter 0,0
PUSHA;write your code here
; mov eax,prompt1
push prompt1
push string_fmt
call _printf;_string
add esp, 4 * 2 ; two parameters at 4 bytes each
; for _scanf, the parameter wants to be
; the address of an integer (dword) variable
; (where to put it)
; not the integer itself
; K&R calls this "the most common error"
push input1
push int_fmt
call _scanf;read_int
add esp, 4 * 2
; the "result" is not what we want!
; mov [input1],eax
; mov eax,prompt2
push prompt2
push string_fmt
call _printf;_string
add esp, 4 * 2
push input2
push int_fmt
call _scanf;read_int
add esp, 4 * 2
; mov [input2],eax
; now we should have our two numbers
; add 'em up
mov eax,[input1]
add eax,[input2]
; copy answer to ebx
; because _printf is going to trash eax!
mov ebx,eax
;dump_regs 1
;dump_mem 2, out_msg1,1
;**********************
;**********************
; mov eax,out_msg1
push out_msg1
push string_fmt
call _printf;_string
add esp, 4 * 2
push [input1]
push int_fmt
; mov eax,[input1]
call _printf;_int
add esp, 4 * 2
; oops! we printed "you entered"
; and then only one number
; we may need a different format string for this
; ignore it for now?
;call printf_n1
; mov eax,[input2]
;call _printf;_int
; mov eax,out_msg2
push out_msg2
push string_fmt
call _printf;_string
add esp, 4 * 2
; mov eax,ebx
; remember that our answer is in ebx
push ebx
push int_fmt
call _printf ;_int
add esp, 4 * 2
;call print_n1
popa
mov eax, 0 ; return back to C
leave
ret
Well... that's untested. It may not even work. At the very least, it'll be "messy". It "may" be enough help so that you can fix it on your own...
If you continue to have trouble, come back here and give us more information. In general, we need to know:
1) What OS? (I'm guessing Windows - Linux doesn't use the underscores on "_main", etc.)
2) What command line did you give Nasm? (Nasm will do many different things. In some cases, you will need completely different code, depending on the output format.)
3) How did you link it? (if at all)
4) What did you "expect" to happen? (that seems obvious in this case - it isn't always)
5) What actually happened? (You say "no input". I'm guessing no output, either. "It doesn't work" is not very helpful. Segfault - what's Windows call it, "access violation"? No output? Garbage output? Etc. There's a lot that can go wrong!)
Edit: Well I see shaynox has answered. Thanks, shaynox! Perhaps between the two answers you can figure something out.
Best,
Frank