这个汇编代码什么都不做 [英] This assembly code makes nothing

查看:28
本文介绍了这个汇编代码什么都不做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个 C 文件,它是这样的:

I'm working in a C file, which is something like this:

    #define v2      0x560000a0
    int main(void)
    {
        long int v1;
        v1 = ioread32(v2);
        return 0;
    }

并且我已经提取了这部分以在汇编中编写它:

and I've extracted this part to write it in assembly:

int main ()
{
    extern v1,v2;
    v1=ioread32(v2);
    return 0;
}

我正在尝试使用 armv4 的汇编代码在 v1 中写入 v2 的值.使用

I'm trying to write the value of v2 in v1 using assembly code for armv4. Using

arm-linux-gnueabi-gcc -S -march=armv4 assembly_file.c

我得到这个代码:

.arch armv4
.eabi_attribute 27, 3
.fpu vfpv3-d16
.eabi_attribute 20, 1
.eabi_attribute 21, 1
.eabi_attribute 23, 3
.eabi_attribute 24, 1
.eabi_attribute 25, 1
.eabi_attribute 26, 2
.eabi_attribute 30, 6
.eabi_attribute 34, 0
.eabi_attribute 18, 4
.file   "txind-rsi.c"
.text
.align  2
.global main
.type   main, %function

main:
@ Function supports interworking.
@ args = 0, pretend = 0, frame = 0
@ frame_needed = 1, uses_anonymous_args = 0
stmfd   sp!, {fp, lr}
add fp, sp, #4
ldr r3, .L2
ldr r3, [r3, #0]
mov r0, r3
bl  ioread32
mov r2, r0
ldr r3, .L2+4
str r2, [r3, #0]
mov r3, #0
mov r0, r3
sub sp, fp, #4
ldmfd   sp!, {fp, lr}
bx  lr

.L3:
.align  2

.L2:
.word   v1
.word   v2
.size   main, .-main
.ident  "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
.section    .note.GNU-stack,"",%progbits

我使用该代码以这种方式将其放回 C 文件中:

I use that code to put it back inside the C file this way:

asm volatile(
    "stmfd  sp!, {fp, lr}\n"
    "add    fp, sp, #4\n"
    "ldr    r3, =v1\n"
    "ldr    r3, [r3, #0]\n"
    "mov    r0, r2\n"
    "ldr    r3, =v2\n"
    "str    r2, [r3, #0]\n"
    "sub    sp, fp, #4\n"
    "ldmfd  sp!, {fp, lr}\n"
    "bx lr"
);

代码没有做任何事情.

事实上,它会停止目标工作.有谁知道为什么?

In fact, it stops the target working. Does anyones know why?

看完你的回答后,我还有一个问题:我如何将常量值放入寄存器中?C 中的代码是这样的:

EDITED: After reading your answers I have another question: How would I put a constant value in a register?. The code in C would be this:

#define v2      0x560000a0
int main(void)
{
    long int value = 0x0000ffff;
    long int v1;
    v1 = ioread32(v2);
    iowrite32(v2,value);
    return 0;
}

我已经试过了:

asm volatile("mov r3, #value");

我收到一条汇编消息:值符号在不同的部分";我也试过

and I get an assembler message: "value symbol is in a different section"; I've also tried

asm volatile("mov r3, #0x0000ffff);

汇编器消息是:修复后无效常量(ffff)".阅读本文后:修复后常量无效?我不知道如何将该值放入 r3 中,因为我似乎无法使用 mov 来实现.我使用的是 armv4,而不是 armv7.链接中提出的这个解决方案对我不起作用:

and the assembler message is: "invalid constant(ffff) after fixup". And after reading this: Invalid constant after fixup? I don't know how I can put that value into r3, as it seems I can't do it with mov.I'm using armv4, not armv7. And this solution proposed in the link, doesn't work for me:

asm volatile("ldr   r3, =#0000ffff\n");

推荐答案

您的代码没有问题,bx lr 是终止 main 的正确方法,没有问题.您的代码很可能由于您访问的地址而崩溃,它可能不是您被允许访问的地址...

There are no problems with your code, bx lr is the proper way to terminate main, no issue there. Your code is most likely crashing due to the address you are accessing, it is probably not an address you are allowed to access...

#define v2      0x560000a0
    int main(void)
    {
        long int v1;
        v1 = ioread32(v2);
        return 0;
    }

如果您在 C 编译步骤上进行优化,您会看到一个更清晰、更简单的版本

if you optimize on the C compile step you can see a cleaner, simpler version

00000000 <main>:
   0:   e92d4008    push    {r3, lr}
   4:   e59f0008    ldr r0, [pc, #8]    ; 14 <main+0x14>
   8:   ebfffffe    bl  0 <ioread32>
   c:   e3a00000    mov r0, #0
  10:   e8bd8008    pop {r3, pc}
  14:   560000a0    strpl   r0, [r0], -r0, lsr #1

这在 asm 中实现起来并不难.

which is not hard to implement in asm.

.globl main
main:
    push {r3,lr}
    ldr r0,=0x560000A0
    bl ioread32
    mov r0,#0
    pop {r3,pc}

组装和拆卸

00000000 <main>:
   0:   e92d4008    push    {r3, lr}
   4:   e59f0008    ldr r0, [pc, #8]    ; 14 <main+0x14>
   8:   ebfffffe    bl  0 <ioread32>
   c:   e3a00000    mov r0, #0
  10:   e8bd8008    pop {r3, pc}
  14:   560000a0    strpl   r0, [r0], -r0, lsr #1

你已经把代码简化到了 v1 变成了死代码的地步,但是函数调用无法优化掉,所以返回值被丢弃了.

you had simplified the code to the point that v1 becomes dead code, but the function call cannot be optimized out, so the return is discarded.

如果你不使用 main 而是创建一个单独的函数来返回

if you dont use main but create a separate function that returns

#define v2      0x560000a0
    long int fun(void)
    {
        long int v1;
        v1 = ioread32(v2);
        return v1;
    }

...该死...尾部优化:

...damn...tail optimization:

00000000 <fun>:
   0:   e59f0000    ldr r0, [pc]    ; 8 <fun+0x8>
   4:   eafffffe    b   0 <ioread32>
   8:   560000a0    strpl   r0, [r0], -r0, lsr #1

哦,好吧.您走在正确的道路上,查看 C 编译器生成的内容,然后模仿或修改它.我怀疑是你的 ioread 是问题而不是外部结构(为什么你用 32 位读取来做 64 位的事情,也许这就是问题很长它很可能会被实现为 64 位).

Oh well. You are on the right path, see what the C compiler generates then mimic or modify that. I suspect it is your ioread that is the problem and not the outer structure (why are you doing a 64 bit thing with a 32 bit read, maybe that is the problem long it is most likely going to be implemented as 64 bit).

这篇关于这个汇编代码什么都不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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