2013-04-27 56 views
0

在NASM我有strconst_0 dw 5, 0, __utf16__('hello')(单反引号)作为字符串从字符串文字中检索单词?

我试图访问这样的“h”(其中,[EBP + 8]是0并且[EBP + 4]是的地址字符串)

mov eax, [ebp+8] ; get the index 
mov ebx, [ebp+4] ; get the address 
movzx eax, word [ebx + 2 * eax + 4] 
push eax 
call print_char ; call the print function 

然而,当我运行此代码,只是一个空字符打印

编辑:全部房源

main: 
pop edx 
push ebp 
mov ebp, esp 
sub esp, 4 
mov [ebp-4], edx 
mov eax, strconst_0 
push eax 
push dword 0 
call getChar 
add esp, 8 
push eax 
call printChar 
add esp, 4 
mov edx, [ebp-4] 
add esp, 4 
pop ebp 
push edx 
ret 

getChar: 
pop edx 
push ebp 
mov ebp, esp 
sub esp, 4 
mov [ebp-4], edx 
mov eax, [ebp+8] 
mov ebx, [ebp+4] 
movzx eax, word [ebx + 2 * eax + 4] 
push eax 
pop eax 
mov edx, [ebp-4] 
add esp, 4 
pop ebp 
push edx 
ret 
+0

OT,但是,为什么这么多的代码来计算地址?尤其是那个双倍宽度的mul ..'movzx eax,word [ebx + 2 * eax + 4]' – harold 2013-04-27 13:32:13

+0

不知道你可以用地址来做 – CallumDev 2013-04-27 13:36:27

+0

不错,对不对?但是这并不能解决这个问题,我不知道什么是错的 – harold 2013-04-27 13:41:33

回答

1

我以错误的方式得到了参数。如果我在初始化mov指令中交换ebx和eax,它可以正常工作。

0
  1. 您在程序开始时将main的返回地址弹出到edx中,这没有任何意义。
  2. 你根本没有评论,所以我不可能理解你想要做什么,因为你没有getChar的原型。

基本上,这里的分析,我不会对我的工具,所以我做了几个假设

main: 
    ; why is return address of main popped into edx? 
    pop  edx 

    ; standard stack setup     
    push  ebp 
    mov  ebp,  esp 

    ; make space for 4 bytes 
    sub  esp,  4 

    ; store edx in where return address used to be... why? 
    mov  [ebp-4], edx 

    ; move address of strconst_0 to eax 
    mov  eax,  strconst_0 

    ; push eax on stack 
    push  eax 

    ; push 4 bytes 0x00000000 on stack 
    push dword 0 

    ; call getchar(0, strconst_0) 
    call  getChar 

    ; restore stack 
    add  esp,  8 

    ; push eax which was mutated in getChar on stack 
    push  eax 

    ; printChar(eax) 
    call  printChar 

    ; restore stack 
    add  esp,  4 

    ; move whatever we overwritten old return address to, to edx 
    mov  edx,  [ebp-4] 

    ; restore stack 
    add  esp,  4 
    pop  ebp 

    ; restore return address 
    push  edx 

    ; return 
    ret 

; what do I accept(on stack) 
; what do I return(in eax) 
getChar: 
    ; again, seems silly 
    pop edx 

    ; obvious 
    push ebp 
    mov  ebp,  esp 

    ; make space for 4 bytes on stack 
    sub  esp,  4 

    ; overwrite return address with edx 
    mov  [ebp-4], edx 

    ; I am guessing you are trying to get the two arguments into eax, ebx 

    ; eax = 0 
    mov  eax,  [ebp+8] 
    ; ebx = strconst_0 
    mov  ebx,  [ebp+4] 
    ; magic is here 
    movzx  eax,  word [ebx + 2 * eax + 4] 
    ; push magic number 
    push  eax 
    ; pop ... something into eax 
    pop  eax 
    ; restore edx from whatever you put there... 
    mov  edx,  [ebp-4] 
    ; restore stack? 
    add  esp,  4 

    ; obvious 
    pop  ebp 
    push  edx 
    ret 

老实说,我不知道你在做什么,如果你说的东西,这将有助于沿着 “我试图做一个函数接受?和字符串缓冲区的地址,哪个?”

特别是, 1.我不知道getChar输入是什么,是字符的位置?这是别的吗? 2.我不知道getChar的输出是什么,它存储在eax中的东西?它发射导弹吗? 3.我不知道为什么你使用你的函数的返回地址临时存储,这是愚蠢的。

main: 
    push   ebp 
    mov   ebp,    esp 

    ; getChar(0, str) 
    push   str 
    mov dword  eax,    0 
    push   eax 
    call   getChar 
    add   esp,    8 

    ; printChar(str) 
    push   str 
    call   printChar 
    add   esp,    4 

    mov   esp,    ebp 
    pop   ebp 
    ret 

; char getChar(long ix, char *str) 
; returns: char at index indicated by ix in eax register 
getChar: 
    push   ebp 
    mov   ebp,    esp 
    push   ebx ; save ebx to avoid unwanted side-effects 

    mov   eax,    [ebp + 8] ; ix 
    mov   ebx,    [ebp + 12] ; str 
    add   eax,    ebx ; eax = &(str[ix]) 
    mov   eax,    (eax) ; eax = *eax = str[ix] 

    pop   ebx ; restore ebx 
    mov   esp,    ebp 
    pop   ebp 
    rts 

str db 'this is a string.', 0 

这是绝对行不通的,并为示范的目的纯粹是给定的,因为我没有在一段时间写的x86,这个想法应该是明确的,但。希望能帮助到你。

+0

getChar接受一个指向字符串的32位指针和一个字符索引。这些字符每个都是2个字节(UTF-16),并且必须以包含大小的双字前缀。 getChar然后在返回之前将该字符放在通过eax传递的索引处。 – CallumDev 2013-04-27 15:10:20

+0

除了你需要乘以(或移位)索引值以使它乘以2(字符串地址+2 *位置)之外,其他想法都是相同的,这就是你的字符位置。然后你将16个字节(所以一个字,而不是一个字节)从这个指针移动到你的eax作为返回值。 – Dmitry 2013-04-27 15:15:34