我正在阅读this assembly tutorial,我得到了工作的例子,所以我决定尝试改进它。一旦我这样做,它就停止了工作,所以我试着将它转换为AT& T语法,这样我就可以用gcc编译它并得到调试信息(因为我在OS X上,并且nasm不会为它生成调试信息Mach-O格式)。
我尝试转换有效的原始程序,但是在我使用AT&amp; T语法编写它之后它停止了工作。我用gdb来调试它,发现我期望包含argc的<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="profileUploadModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Upload profilepicture</h4>
</div>
<div class="modal-body" style="text-align: center;">
<div class="bbody">
<!-- upload form -->
<form id="upload_form" enctype="multipart/form-data" method="post" action="upload.php" onsubmit="return checkForm()">
<!-- hidden crop params -->
<input type="hidden" id="x1" name="x1" />
<input type="hidden" id="y1" name="y1" />
<input type="hidden" id="x2" name="x2" />
<input type="hidden" id="y2" name="y2" />
<h2>Step1: Please select image file</h2>
<div>
<input type="file" name="image_file" id="image_file" onchange="fileSelectHandler()" />
</div>
<div class="error"></div>
<div class="step2">
<h2>Step2: Please select a crop region</h2>
<img id="preview" />
<div class="info">
<label>File size</label>
<input type="text" id="filesize" name="filesize" />
<label>Type</label>
<input type="text" id="filetype" name="filetype" />
<label>Image dimension</label>
<input type="text" id="filedim" name="filedim" />
<label>W</label>
<input type="text" id="w" name="w" />
<label>H</label>
<input type="text" id="h" name="h" />
</div>
<input type="submit" value="Upload" />
</div>
</form>
</div>
</div>
</div>
</div>
</div>
包含值%rsp
。英特尔语法版本不会发生这种情况。
所以我的问题是,为什么0x93b2e5ad
在AT&amp; T版本中的argc值不正确,而在英特尔版本中却没有?
这是原始的英特尔语法版本(我稍微修改了教程中的版本):
%rsp
以下是我将其转换为AT&amp; T语法后的代码:
section .data
WRONG_ARGC db "Must be two command line argument", 0xa
WRONG_ARGC_SIZE equ 34
section .text
global start
start:
pop rcx
cmp rcx, 3
jne argcError
add rsp, 8
pop rsi
call str_to_int
mov r10, rax
pop rsi
call str_to_int
mov r11, rax
add r10, r11
mov rax, r10
xor r12, r12
jmp int_to_str
argcError:
mov rax, 0x2000004
mov rdi, 1
mov rsi, WRONG_ARGC
mov rdx, WRONG_ARGC_SIZE
syscall
jmp exit
; set rdi to exit status before calling
exit:
mov rax, 0x2000001
syscall
str_to_int:
xor rax, rax
mov rcx, 10
next:
cmp [rsi], byte 0
je return_str
mov bl, [rsi]
sub bl, 48
mul rcx
add rax, rbx
inc rsi
jmp next
return_str:
ret
int_to_str:
mov rdx, 0
mov rbx, 10
div rbx
add rdx, 48
; push rdx
dec rsp
mov rsp, rdx
inc r12
cmp rax, 0
jne int_to_str
jmp print
print:
; mov rax, 1
; mul r12
mov rax, r12
; mov r12, 8
; mul r12
mov rdx, rax
mov rax, 0x2000004
mov rdi, 1
mov rsi, rsp
syscall
mov rdi, 0
jmp exit
我编译了这样的英特尔版本:
.data
WRONG_ARGC: .ascii "Must be two command line argument\n"
WRONG_ARGC_SIZE: .quad 34
.text
.globl _main
_main:
popq %rcx
cmpq $3, %rcx
jne argcError
addq $8, %rsp
popq %rsi
call str_to_int
movq %rax, %r10
popq %rsi
call str_to_int
movq %rax, %r11
addq %r11, %r10
movq %r10, %rax
xorq %r12, %r12
jmp int_to_str
argcError:
movq $0x2000004, %rax
movq $1, %rdi
movq WRONG_ARGC@GOTPCREL(%rsi), %rsi
movq WRONG_ARGC_SIZE@GOTPCREL(%rsi), %rdx
syscall
jmp exit
# set rdi to exit status before calling
exit:
movq $0x2000001, %rax
syscall
str_to_int:
xorq %rax, %rax
movq $10, %rcx
next:
cmpq $0, (%rsi)
je return_str
movb (%rsi), %bl
subb $48, %bl
mulq %rcx
addq %rbx, %rax
incq %rsi
jmp next
return_str:
ret
int_to_str:
movq $0, %rdx
movq $10, %rbx
divq %rbx
addq $48, %rdx
push %rdx
#decq %rsp
#movq %rdx, %rsp
incq %r12
cmpq $0, %rax
jne int_to_str
jmp print
print:
movq $1, %rax
mulq %r12
movq %r12, %rax
movq $8, %r12
mulq %r12
movq %rax, %rdx
movq $0x2000004, %rax
movq $1, %rdi
movq %rsp, %rsi
syscall
movq $0, %rdi
jmp exit
我编译了这样的AT&amp; T版本:
nasm -f macho64 -o add_intel.o add_intel.s
ld -o add_intel add_intel.s