x86-64阵列输入和打印 [英] x86-64 Arrays Inputting and Printing

查看:109
本文介绍了x86-64阵列输入和打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将值输入到x86-64 Intel程序集中的数组中,但我不太清楚。



我正在创建一个段.bss中的数组。然后,我尝试使用r15将数组的地址传递给另一个模块。在该模块内,我提示用户输入一个数字,然后将其插入数组。



我正在尝试执行以下操作

  segment .bss 
dataArray resq 15;

segment操纵的数组.text
mov rdi,dataArray;存储阵列的内存地址,以便下一个模块可以使用它。
调用inputqarray;调用inputqarray模块

在inputqarary内部,我有:

  mov r15,rdi;将数组的内存地址移到r15中,以确保

push qword 0的安全;在堆栈上腾出空间以获取我们正在读取的值
mov rsi,rsp;将第二个参数设置为指向堆栈
mov rax,0上的新位置。没有上证所输入
mov rdi,oneFloat; %f,0
调用scanf;调用C标准库scanf函数
调用getchar;清理输入流

pop qword [r15]

然后我尝试通过执行

  push qword输出使用者输入的值0 
mov rax,1
mov rdi ,oneFloat
movsd xmm0,[dataArray]
调用printf
pop rax

不幸的是,我得到的输出都是0.00000

解决方案

输出为0,因为使用的格式说明符错误。它应该是%lf
接下来,无需在过程中推送和弹出。由于您要将数据数组的地址传递给 scanf ,而该地址将位于 rsi 中,因此只需将其传递在 rsi 中;



您声明数组为15个QWORDS,对吗-120个字节?还是您说的是 resb 15



这有效,应该会帮助您:

 外部printf,scanf,退出
全局main

段.rodata
fmtFloatIn db% lf,0
fmtFloatOut db`%lf\n`,0

section .bss
dataArray resb 15

section .text
main:
sub rsp,8;堆栈指针16字节对齐

mov rsi,dataArray
调用inputqarray

movsd xmm0,[dataArray]
mov rdi,fmtFloatOut
mov rax,1
调用printf

调用出口

inputqarray:
sub rsp,8;堆栈指针16字节对齐

;指向缓冲区的指针位于rsi
mov rdi,fmtFloatIn
mov rax中,0
调用scanf

添加rsp,8
ret



由于您要将rdi中的参数传递给C函数,因此Windows上没有此功能。


I'm trying to input values into an array in x86-64 Intel assembly, but I can't quite figure it out.

I'm creating an array in segement .bss. Then I try to pass the address of the array along to another module by using r15. Inside that module I prompt the user for a number that I then insert into the array. But it doesn't work.

I'm trying to do the following

segment .bss
dataArray resq 15                                       ; Array that will be manipulated

segment .text
mov rdi, dataArray                                      ; Store memory address of array so the next module can use it.
call inputqarray                                        ; Calling inputqarray module

Inside of inputqarary I have:

mov r15, rdi                                            ; Move the memory address of the array into r15 for safe keeping

push qword 0                                            ; Make space on the stack for the value we are reading
mov rsi, rsp                                            ; Set the second argument to point to the new locaiton on the stack
mov rax, 0                                              ; No SSE input
mov rdi, oneFloat                                       ; "%f", 0
call scanf                                              ; Call C Standard Library scanf function
call getchar                                            ; Clean the input stream

pop qword [r15]

I then try to output the value entered by the use by doing

push qword 0
mov rax, 1
mov rdi, oneFloat
movsd xmm0, [dataArray]
call printf
pop rax

Unfortunately, all I get for output is 0.00000

解决方案

Output is 0 because you are using the wrong format specifier. It should be "%lf" Next, no need to push and pop in your procedure. Since your going to pass the address of the data array to scanf, and that will be in rsi, just pass it in rsi; one less move.

You declared your array as 15 QWORDS, is that correct - 120 bytes? or did you mean resb 15?

This works and should get you on your way:

extern printf, scanf, exit
global main

section .rodata
fmtFloatIn      db  "%lf", 0
fmtFloatOut     db  `%lf\n`, 0

section .bss
dataArray       resb 15

section .text
main:
    sub     rsp, 8                          ; stack pointer 16 byte aligned

    mov     rsi, dataArray
    call    inputqarray

    movsd   xmm0, [dataArray]
    mov     rdi, fmtFloatOut
    mov     rax, 1
    call    printf

    call    exit

inputqarray:
    sub     rsp, 8                          ; stack pointer 16 byte aligned

    ; pointer to buffer is in rsi
    mov     rdi, fmtFloatIn
    mov     rax, 0
    call    scanf

    add     rsp, 8
    ret

Since you are passing params in rdi to the C functions, this is not on Windows.

这篇关于x86-64阵列输入和打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆