简体   繁体   中英

How to read a 64-bit number in assembly

I have to read a 64-bit number from the keyboard,but I do not know why it does not work. Could anyone give me an idea?I am working in masm. This is what i did:

 key dq 0
 give_key db "Enter the encryption key (64-bit): " , 0
 formatkey DB "%lld ", 0

procedure PROC NEAR
    push offset give_key
    call printf
    add esp,4
    push offset key
    push offset formatkey
    call scanf
    add esp,8
    ret
procedure ENDP

If you're using x86 assembly, you will have to do two mov s, one for the lower 32 bits, and one for the upper. On x64, you can move it in one instruction, which means on 64-bit, the operation is atomic, whereas on x86, it's not.

For instance, on x86:

mov dword [eax], low32
mov dword [eax+4], high32

And on x64:

mov rax, 0xffffffffffffffff

The last space in formatkey causes trouble. Do you really need it?

This works for me ( .lib from Visual Studio 2010 Express):

includelib msvcrt.lib

.686
.MODEL flat
EXTERN _printf:proc, _scanf:proc, _exit:proc

.DATA

    key dq 0
    give_key db "Enter the encryption key (64-bit): " , 0
    scanformat DB "%lld", 0
    printformat DB 10, "Entered: %lld", 10, 0

.CODE

procedure PROC NEAR
    push offset give_key
    call _printf
    add esp,4
    push offset key
    push offset scanformat
    call _scanf
    add esp,8
    ret
procedure ENDP

_main PROC

    call procedure

    push dword ptr key + 4          ; High DWORD of 64-bit QWORD
    push dword ptr key + 0          ; Low DWORD of 64-bit QWORD
    push offset printformat
    call _printf
    add esp,12

    push 0
    call _exit

_main ENDP

END _main

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM