2014-01-07 1 views
0

저는 ASM을 처음 접했습니다. 나는 ARCH 리눅스 64 비트를 실행하고 compiule 다음 명령을 사용하여 모든 것이 원활하게 실행 해요 : 나는 (소수로) 사용자의 입력을 복용하고인텔 ASM EAX에 추가

nasm -f elf32 -o file.o file.asm 
ld -s -m elf_i386 -o file file.o 

나는 단순히 그것을 두 배로합니다. 그러나, 내가 사용할 때 :

add eax, eax 

나는 아무 것도 얻지 못한다. 나는 그 다음 시도했다 :

add eax, 1 

어느 것이 eax에 1을 더해야하는지. 그러나 이것은 메모리 주소에 1을 더합니다. 내 .bss 섹션에서 10 바이트를 예약했습니다.

나는 "1234"은으로 출력한다 입력 "234"반대 (1 바이트를 이동) "1235"

.bss라고 섹션 :

i: resd 
    j: resd 10 

전체 문 :

mov eax, 3 ;syscall 
    mov ebx, 2 ;syscall 
    mov ecx, i ;input variable 
    mov edx, 10 ;length of 'i' 
    int 0x80 ;kernel call 

    mov eax, i ;moving i into eax 
    add eax, 0x1 ;adding 1 to eax 
    mov [j], eax ;moving eax into j 

답변

0

조작중인 것은 숫자의 문자열 표현입니다.

당신은 아마 수행해야합니다

  1. 숫자
  2. 더블
  3. 문자열로 다시 결과 숫자로 변환 숫자를 문자열로 변환

그래서 (테스트되지 않은 코드 같은 앞서) :

mov esi,i ; get address of input string 
; convert string to a number stored in ecx 
xor ecx,ecx ; number is initially 0 
loop_top: 
mov bl,[esi] ; load next input byte 
cmp bl,0 ; byte value 0 marks end of string 
je loop_end 
sub bl,'0' ; convert from ASCII digit to numeric byte 
movzx ebx,bl ; convert bl to a dword 
mov eax,10 ; multiply previous ecx by 10 
mul ecx 
mov ecx,eax 
add ecx,ebx ; add next digit value 
inc esi ; prepare to fetch next digit 
jmp loop_top 
loop_end: 
; ecx now contains the number 
add ecx,ecx ; double it 
; to do: convert ecx back to a string value 

Page 22 of this document에는 atoi 기능의 NASM 구현이 포함되어 있습니다.

관련 문제