Author Topic: Display result  (Read 24356 times)

Offline assistech

  • Jr. Member
  • *
  • Posts: 27
Display result
« on: April 13, 2020, 12:23:57 PM »
Hi,


I'm programming a simple program and running Windows 10 or 7, i would like to display the result of my operation on CMD after to execute it. This is my program:


global _main:

section .text
          extern printf

_main:

     mov eax,10000h
     add eax,40000h
     sub eax,20000h
 
     push eax
     call printf



What is wrong ????

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Display result
« Reply #1 on: April 14, 2020, 12:45:44 AM »
Hi assistech.

Welcome to the forum.

You don't say what's wrong. I don't do Windows. Wild asm guess:

You need to spell "_printf" with a leading underscore.
You need a format string.
You need to clean up stack and "ret".

Hope that helps.

Best,
Frank


Offline assistech

  • Jr. Member
  • *
  • Posts: 27
Re: Display result
« Reply #2 on: April 15, 2020, 12:53:17 AM »
Ok, if i write this complete code from SASM editor, could you correct or add instruction for display EAX result for me please:


%include "io.inc"

section .data

section .text

      global CMAIN

CMAIN:
      mov ebp,esp  ;for correct debugging
      NEWLINE

      mov eax,10h
      mov eax, 40h
      sub eax,20h

      xor eax,eax

      ret


Where could put "_printf" ?


Thanks


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Display result
« Reply #3 on: April 15, 2020, 02:14:52 AM »
Warning! Untested code!

Code: [Select]
%include "io.inc"

extern _printf

section .data

    fmt db "result is %d", 10, 0

section .text

      global CMAIN

CMAIN:
      mov ebp,esp  ;for correct debugging
      NEWLINE

      mov eax,10h
      mov eax, 40h
      sub eax,20h

    push fmt
    push eax
    call _printf
    add esp, 8

      xor eax,eax

      ret

Doggone it! Where do I find "io.inc'? Well, SASM I guess...
Something like that...

Doggone it! Someone doing 'doze help this guy!

Best,
Frank

P.S. "code" in square brackets, "/code" in square brackets...


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Display result
« Reply #4 on: April 15, 2020, 03:30:31 AM »
Doggone! Push eax first, then fmt. Sorry/

Best,
Frank



Offline assistech

  • Jr. Member
  • *
  • Posts: 27
Re: Display result
« Reply #5 on: April 15, 2020, 11:32:00 AM »
Great, Great, Great......


It's seem that is instruction "add esp,8" which allow to display on the screen the result!!!!

Can you explain me why ? What is ESP ?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Display result
« Reply #6 on: April 15, 2020, 09:30:22 PM »
Hi assistech,

ESP is the (32 bit) stack pointer. The stack is an area of memory set aside for the push and pop instructions. It works from high memory to low. It is a;so used by the call instruction to srore the address to return to when the call returns. When the "ret" instruction occurs, it takes the first thing on the stack (upwards) and goes there. In this case, the "C startup code" (which we don't see) calls _main. When our code returns, it exits cleanly back to the OS. We push eax and the address of "fmt" onto the stack (below the return address). These parameters have to be removed for our code to return to its caller. C functions do not remove their parameters... but Windows APIs do. Are you confused yet? You need a good book, and I really don't know what to recommend. Maybe Jeff Duntemann?

Best,
Frank


Offline assistech

  • Jr. Member
  • *
  • Posts: 27
Re: Display result
« Reply #7 on: April 18, 2020, 12:19:04 PM »
Ok, i'm understand:


1- But why the value "8" inside instruction "add esp,8", why not "16" or "24" ?

2- I have ever old book about Assembly like MASM which uses this for display registers: "call DumpRegs".

3- The problem is after looking for everywhere (books, article,website,...) response about display registers, n o one give explainations about it except you or MASM with call DumpRegs. Nasm doc don't talk about it "add esp,8", how can i know ? That's why i had abandon Assembly langage

4- But i will looking for Jeff Duntemann books later.

Offline assistech

  • Jr. Member
  • *
  • Posts: 27
Re: Display result
« Reply #8 on: April 18, 2020, 02:56:19 PM »
For example:


How do you break a line with NASM for printing another string ?

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: Display result
« Reply #9 on: April 18, 2020, 04:02:15 PM »
1- But why the value "8" inside instruction "add esp,8", why not "16" or "24" ?
Because, previously, you pushed 8 bytes on the stack:
Code: [Select]
  push fmt    ; 4 bytes pushed.
  push eax    ; 4 bytes pushed.
  call _printf
  add esp, 8

Quote from: assistech
2- I have ever old book about Assembly like MASM which uses this for display registers: "call DumpRegs".

3- The problem is after looking for everywhere (books, article,website,...) response about display registers, n o one give explainations about it except you or MASM with call DumpRegs. Nasm doc don't talk about it "add esp,8", how can i know ? That's why i had abandon Assembly langage

4- But i will looking for Jeff Duntemann books later.
The problem with old books about x86 assembly is that their examples are outdated and, probably, using a custumized library. On Win32/Win64 you must use Console API functions like GetStdHandle() and WriteConsole() and to exit your program, ExitProcess() Win API function.

Windows 10 has no support whatsoever to old MS-DOS console apps.

You'll have to do some work to import the Win API functions to your console app. So, I don't recomment to write entire programs in assembly, since a C/C++ compiler will do this work for you.

Ahhh... and, nowadays, I recommend you try to use x86-64 mode, not the old i386 (32 bits) mode... Almost everyone is using 64 bits mode now (different calling convention, some different rules to register usage, more registers available, etc...).
« Last Edit: April 18, 2020, 04:08:10 PM by fredericopissarra »

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: Display result
« Reply #10 on: April 18, 2020, 04:06:20 PM »
How do you break a line with NASM for printing another string?

Print "\r\n" string to the console.

Offline assistech

  • Jr. Member
  • *
  • Posts: 27
Re: Display result
« Reply #11 on: April 18, 2020, 05:39:51 PM »

Sorry,

NASM seems to not recognize Print"\r\n"

Offline assistech

  • Jr. Member
  • *
  • Posts: 27
Re: Display result
« Reply #12 on: April 18, 2020, 06:02:04 PM »
Great,


I have understanded now for "add esp,8". Thanks

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: Display result
« Reply #13 on: April 18, 2020, 06:21:01 PM »
NASM seems to not recognize Print"\r\n"
Using Console API, assistech...

Offline assistech

  • Jr. Member
  • *
  • Posts: 27
Re: Display result
« Reply #14 on: April 18, 2020, 07:16:04 PM »
Great,

i used SASM, that's work....


I'm understand faster with you than book,  thanks