G
G
Grandma Luda2022-03-07 13:40:38
assembler
Grandma Luda, 2022-03-07 13:40:38

My program does not display numbers in the console, how can I fix it?

My program doesn't make sense, it should get 2 numbers and print them out, but it outputs the address in place of the first number and the letter in place of the second number. The program is written using FASM

format PE console

entry start

include 'win32a.inc'

section '.data' data readable writable
        formatNum db '%d', 0
        formatNum2 db '%t', 0

        razmer rb 1
        razmer2 rb 1

        wn db 'RY? ', 0
        ho db 'SY? ', 0
        tg db ' %d, %t', 0

        NULL = 0

section '.code' code readable executable

        start:
                push wn
                call [printf]

                push razmer
                push formatNum
                call [scanf]

                push ho
                call [printf]

                push razmer2
                push formatNum2
                call [scanf]

                push razmer2
                push razmer
                push tg
                call [printf]

                call [getch]

                push NULL
                call [ExitProcess]

section '.idata' import data readable
        library kernel , 'kernel32.dll',\
                msvcrt, 'msvcrt.dll'

        import kernel,\
                ExitProcess, 'ExitProcess'

        import msvcrt,\
               printf, 'printf',\
               getch, '_getch',\
               scanf, 'scanf'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2022-03-07
@jcmvbkbc

My program ... displays the address in place of the first number and the letter in place of the second number.
how to fix?

1) see what parameters the printf function you are using expects. The format string for two integers is "%d %d".
2) learn how parameters are passed to a function and what to do after returning from it. You use push razmerto pass the address of the razmer variable to scanf, but in printf you need to pass not the address of the variable, but its value. For example like this:
mov eax, [razmer]
push eax

In addition, there are other errors in the code: razmer and razmer2 are defined as rb 1, but should be defined as rd 1, since the %d format for scanf will result in 4 bytes being written. %t -- no such format in scanf, probably meant %d.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question