2014-09-23 68 views
0
global _start 

section .data 

section .bss  ;declares 3 variables 
num1: resb 4 
num2: resb 4 
sum: resb 4 
section .text 

_start: 
mov  ecx, num1 
mov  edx, 02h 

call  read 
call  write 

mov  ecx, num2 
mov  edx, 02h 

call  read2 
call  write2 

;mov  ecx, sum 
;mov  edx, 02h 

mov  ecx, num1 
add  ecx, num2 
mov  sum, ecx 

je  exit 

exit: ;exits the program 
mov  eax, 01h  ; exit() 
xor  ebx, ebx  ; errno 
int  80h 

read: ;reads input from the keyboard and stores it in ecx 
mov  eax, 03h  ; read() 
mov  ebx, 00h  ; stdin 
mov  ecx, num1 
int  80h 
ret 

read2: ;reads input from the keyboard and stores it in ecx 
mov  eax, 03h  ; read() 
mov  ebx, 00h  ; stdin 
mov  ecx, num2 
int  80h 
ret 

write: ;outputs the contents of ecx to stdout 
mov  eax, 04h  ; write() 
mov  ebx, 01h  ; stdout 
mov  ecx, num1 
int  80h 
ret 

write2: ;outputs the contents of ecx to stdout 
mov  eax, 04h  ; write() 
mov  ebx, 01h  ; stdout 
mov  ecx, num2 
int  80h 
ret 

我需要添加2個變量一起幫忙。我一直從線mov sum, ecx接收invalid combination of opcode and operands錯誤。這感覺就像我嘗試了幾十種組合,沒有真正的運氣。一旦我得到加到總和變量的變量我還需要將結果打印到stdoutNASM:如果添加2個變量

回答

0

你得到的錯誤,因爲你試圖ECX移動立即數這是不可能的。

mov  ecx, num1 ; num1 is the address of the "num1" label 
add  ecx, num2 ; num2 is the address of the "num2" label 
mov  sum, ecx ; sum is the address of the "sum" label 

所以根據標籤的地址,你的代碼轉換爲類似:

mov  ecx, 0x401800 
add  ecx, 0x401804 
mov  0x401808, ecx 

...你可能不希望。 如果您想使用地址中的內容,您必須使用方括號:

mov  ecx, [num1] ; get contents at "num1" 
add  ecx, [num2] ; get contents at "num2" 
mov  [sum], ecx ; move ecx to address "sum"