2012-10-05 47 views
0

我是GAS部件的新手,我的目標是顯示用戶輸入的兩個數字之和。我用char轉換數字,反之亦然,結果總是錯誤的(非數字)。有人可以告訴我這個錯誤在哪裏嗎?提前致謝。在氣體中添加兩個數字

.section .data 
prompt_str1: 
    .ascii "Enter first number: " 
str1_end: 
    .set STR1_SIZE, str1_end-prompt_str1 
prompt_str2: 
    .ascii "Enter second number: " 
str2_end: 
    .set STR2_SIZE, str2_end-prompt_str2 

.section .bss 
.lcomm  input1 2 
.lcomm  input2 2 
.lcomm  ans  1 

.macro write str,str_size 
    movl $4, %eax 
    movl $1, %ebx 
    movl \str, %ecx 
    movl \str_size, %edx 
    int $0x80 
.endm 

.macro writenum str 
    movl $4, %eax 
    movl $1, %ebx 
    movl \str, %ecx 
    int $0x80 
.endm 

.macro read buff, buff_size 
    movl $3, %eax 
    movl $0, %ebx 
    movl \buff, %ecx 
    movl \buff_size, %edx 
    int $0x80 
.endm 

.section .text 
.globl _start 

_start: 
write $prompt_str1, $STR1_SIZE 
read $input1, $2 

write $prompt_str2, $STR2_SIZE 
read $input2, $2 

subl $0x30, input1 
subl $0x30, input2 
movl $input1, %eax 
addl $input2, %eax 
movl %eax, ans 
addl $0x30, ans 
writenum $ans 

exit: 
movl $1, %eax 
movl $0, %ebx 
int $0x80 
+1

你試過用gdb調試你的代碼嗎?如果你不是爲什麼? –

回答

1

movl $input1, %eax 
addl $input2, %eax 

你正在移動的輸入1的地址爲%EAX和添加輸入2的地址。失去$

這段代碼可能有更多的錯誤。由於您只能處理單個數字(包括總和!),因此您應該在此處使用字節,而不是「long」,但只需轉儲$即可獲得至少一個數字結果。

地址與「該地址的內容」之間的區別是非常重要的!不幸的是,每個彙編程序都以不同的方式......嘆息...