尝试在 NASM 结构体的数组中的某个点获取值,但未返回正确的值,但仅针对某些索引返回正确的值

问题描述 投票:0回答:1

我一直在尝试访问结构中数组中某个点的值。对于 3 个索引值,这只返回一个非常大的数字(随着索引因某种原因上升而变得越来越小),并开始返回索引 3 及以上的正确值。

这是我的代码:(请看最后的问题)

extern printf
global main

section .data
format_specifierSTOCK:
db 'Stock: %d', 10, 0
format_specifierSOLD:
db 'Sold: %d', 10, 0
format_specifierSTR:
db 'Flavor: %s', 10, 0
format_specifierFLT:
db 'Price: %f', 10, 0
format_specifierArray1:
db 'BestsellingHours: %d', 10, 0

struc GatoradeSellingDay
gatoradeType resb 12
stock resb 4
numSold resb 4
price resw 4
TimesWhenGatoradeSellsWell resb 5
endstruc

DayOne istruc GatoradeSellingDay
at gatoradeType, db "Lemon Lime",
at stock, db 20,
at numSold, db 0
at price, dq 3.50,
at TimesWhenGatoradeSellsWell, db 9, 10, 11, 12, 13
iend

section .text

main:


mov r12, [DayOne + stock]
dec r12
mov [DayOne + stock], r12

mov r11, [DayOne + numSold]
inc r11
mov [DayOne + numSold], r11

mov rdi, format_specifierSTR
mov rsi, DayOne + gatoradeType
xor rax, rax
call printf

mov rdi, format_specifierSOLD
mov rsi, [DayOne + numSold]
xor rax, rax
call printf

mov rdi, format_specifierSTOCK
mov rsi, [DayOne + stock]
xor rax, rax
call printf

push rbp
movsd xmm0, [DayOne + price]
mov rdi, format_specifierFLT
mov rax, 1
call printf
pop rbp

mov rdi, format_specifierArray1
mov rsi, [DayOne + TimesWhenGatoradeSellsWell + 2] ;right here i am trying to access the index 2 of the array, this returns 855051 instead of 11. if the number is 3 or 4 in this statement, it will corectly return 12 and 13 respectively. 
xor rax, rax
call printf
linux assembly nasm
1个回答
0
投票

Jester 的评论就是答案。基本上在装配中,尺寸很重要。用 db 定义的字节数组就是字节数组。以十六进制表示为:

0x09、0x0a、0x0b、0x0c、0x0d

从该数组的 +2 元素开始并将 64 位读取到 rsi 中,寄存器最终将是:

0x0b、0x0c、0x0d、0x??、0x??、0x??、0x??、0x??

由于处理器读取小端并猜测未定义的字节恰好为零,这意味着 rsi 最终为:

0x00000000000d0c0b

十进制为 855,051。解决方案是从 db 定义的字节数组中仅读入一个字节,并对其进行零扩展。因此使用:

movzx esi,字节 [DayOne + TimesWhenGatoradeSellsWell + 2]

调试汇编代码时,查看十六进制值通常很有用,因为您可以在意外结果中识别值 11、12、13(又名 0x0b、0x0c、0x0d)等模式,这可以帮助您诊断问题所在。

© www.soinside.com 2019 - 2024. All rights reserved.