NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: assistech on April 26, 2020, 01:41:33 PM

Title: Keep value in memory after "call _scanf"
Post by: assistech on April 26, 2020, 01:41:33 PM
Hi,

After to hit a value for a "call _scanf", how could you keep or use it for other operations ?


%include "io.inc"

extern _printf
extern _scanf
extern _puts

section .data
           msg dd 'The number is %d',13,10,0           

section .text
global CMAIN

CMAIN:

mov ebp,esp;

push eax
push msg
call _puts
NEWLINE

call _scanf
add esp,16


xor eax,eax
ret

Title: Re: Keep value in memory after "call _scanf"
Post by: Frank Kotler on April 26, 2020, 08:21:31 PM
What have you tried?

I believe "scanf" should take a format string and one or more pointer arguments. Try
man 3 scanf" (or however you do it in your OS). The result should be where you tell it to be.

Best,
Frank

Title: Re: Keep value in memory after "call _scanf"
Post by: assistech on April 26, 2020, 08:54:53 PM
Hi,

I would like "2" in a variable "i" for storing it after scanf ?

like (C program):

int main(void)
{
int i;
char c;

printf("your score \n");
scanf("%d",&i);

printf("this is your score %d",i);

c=getchar();
}

Title: Re: Keep value in memory after "call _scanf"
Post by: Frank Kotler on April 26, 2020, 09:04:24 PM
W hat have you tried? What happened?

Best,
Frank

Title: Re: Keep value in memory after "call _scanf"
Post by: assistech on April 26, 2020, 09:11:37 PM
Hi sorry, my question is very simple maybe bad explanation sorry for that:


In my first program after "call _scanf", i would like to store the value in variable for another exploitation.

Please
Title: Re: Keep value in memory after "call _scanf"
Post by: assistech on April 26, 2020, 09:19:45 PM
Maybe print it again with call _printf
Title: Re: Keep value in memory after "call _scanf"
Post by: Frank Kotler on April 26, 2020, 09:41:37 PM
Quote
my question is very simple

So is mine:
What have you tried?

Best,
Frank

Title: Re: Keep value in memory after "call _scanf"
Post by: assistech on April 26, 2020, 09:45:57 PM
Can you write this simple C program in Assembler please:


int main(void)
{
int i;
char c;

printf("your score \n");
scanf("%d",&i);

printf("this is your score %d",i);

c=getchar();
}


*NB: i will happy to understand how you take out "i".....
Title: Re: Keep value in memory after "call _scanf"
Post by: assistech on April 26, 2020, 10:02:09 PM
Have you understand ? After to hit my value with _scanf, i would like to display it again at screen
Title: Re: Keep value in memory after "call _scanf"
Post by: Frank Kotler on April 27, 2020, 03:26:02 AM
I have another question. If you're not willing to put any effort into this, why should I?

Oh alright... you did say "please"...

This is for Linux. I'm not going back to  Windows even if you say 'Pretty please with sugar on it'!

The "--prefix _" should put underscores on "main", "scanf", and "printf" and make it work for 'doze. I hope...

I made "t" a global variable, not local/automatic as your C program showed. Simpler, IMO. If you want it local, TRY IT!

Code: [Select]
; Linux nasm -f elf32 prog.asm
; 'doze nasm -f win32 --prefix _ prog.asm
; gcc -m32  -o prog(.exe)  prog.o
         
global main
extern scanf
extern printf
         
section .text
main:   
         
    push ebp
    mov ebp, esp
         
    push t
    push fmt
    call scanf

    push dword [t]
    push fmt
    call printf

    mov esp, ebp
    pop ebp
    ret

    fmt db "%d", 0

section .bss
    t resd 1

Best,
Frank

Title: Re: Keep value in memory after "call _scanf"
Post by: assistech on May 04, 2020, 05:58:07 PM
Hi, thanks


Yes, i'm doing efforts, but sometimes i need to compare experiment codes. Now, i have more skill.... Thanks


But something have no explanation like this :

1- push dword[t] ????    (What is "dword[---]" ?), is it a Pointer ??



Is dword point on one word, if i have a string like "good morning", can i use dword[t] ?
Title: Re: Keep value in memory after "call _scanf"
Post by: Frank Kotler on May 05, 2020, 03:39:22 AM
"dword" is the size. "doubleword" - 4 bytes - 32 bits

If you used it on your string.you would get only the 4 bytes "good", Probably not what you want.

Best,
Frank
Title: Re: Keep value in memory after "call _scanf"
Post by: assistech on May 06, 2020, 12:14:58 PM
Hi,


Have you ever use "scanf" with NASMX ? Will write like it : INVOKE message,scanf



Thanks
Title: Re: Keep value in memory after "call _scanf"
Post by: Frank Kotler on May 06, 2020, 08:51:02 PM
I don't think so. I don't know much about NASMX and rarely use the C library...

Best,
Frank

Title: Re: Keep value in memory after "call _scanf"
Post by: assistech on May 06, 2020, 11:08:22 PM
Ok,


You never use "procedure", "structure" ??
Title: Re: Keep value in memory after "call _scanf"
Post by: Frank Kotler on May 06, 2020, 11:19:35 PM
"structure" yes (rarely). "procedure"... probably not.

Best.
Frank