- プログラムの開始時に main の戻りアドレスを edx にポップすると、意味がありません。
- 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 を書いていないので、純粋にデモンストレーションの目的で提供されていますが、アイデアは明らかなはずです。それが役に立てば幸い。