gpt4 book ai didi

c - 在Y86汇编中如何为多个数组分配好空间?

转载 作者:行者123 更新时间:2023-11-30 15:49:58 25 4
gpt4 key购买 nike

我正在尝试将 C 代码转换为 Y86 汇编代码。

如果您有多个数组声明,会发生什么情况,例如:

int a[100], b[100];

假设每个整数都是 4 个字节。你怎么知道 pos 指向内存中的哪个位置。指令,这样你就不会浪费任何空间?

<Assembly code begins here>

...

halt

# Array initialization begins here
.pos ?
A:
.long 0

.pos ?
B:
.long 0

最佳答案

让汇编器担心偏移量。
一种可能性是定义一个包含局部变量的结构

struc Locals
a dd ? dup 100
b dd ? dup 100
ends Locals

sub esp, sizeof Locals ;; or perhaps sizeof struc Locals
mov ebp, esp ;; take a copy of stack ptr

mov eax, [ebp + offset a] ;;
mov ebx, [ebp + offset b] ;;

在gcc中,“struct”的含义是修改当前段的绝对位置,而不引入可链接代码:

.file "temp.c"
.struct 0
a: .struct a + 4*100
b: .struct b + 4*100
c: ;; c will contain expression for the size
.struct 0 ;; you can start a new struct here
a2: .struct a2 + 4 ;; this struct would be 'int a2;'
b2: .struct b2 + 8 ;; 'double b2'
c2: ;; sizeof struct #2

.text
sub c, %rsp
mov $13, a(%rsp) ;; a[0] = 13
mov $2, b(%rsp) ;; b[0] = 2

关于c - 在Y86汇编中如何为多个数组分配好空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16006737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com