我编写了这个汇编代码来确定用户输入的两个字符串之间的汉明距离。我对汇编一点也不熟悉,这是我除了写helloWorld之外的第一次尝试。
我认为除了打印两个字符串之间的距离之外,代码应该是正确的。
extern printf
extern sscanf
extern fscanf
section .data ; instatiating constants ( variables )
msg1: db "Enter string to be compared less than 256 characters:",10 ; db allocates
;space in memory
len1: equ $-msg1
msg2 db "Enter another string to be compared:",10 ;
len2: equ $-msg2
count: dd 0 ;for the hamming code loop
; another string for "hamming distance"
; maybe make an error message
section .bss ; reserve space for data
;; the bss section typically includes all uninitialized variables, as
;; well as uninitialized variables declared STATIC
input1: resb 256 ; 256 is max length of buffer
input2: resb 256 ; " "
length1: resb 4 ; reserve length of first string
length2: resb 4 ; reserve lenght of second string
section .text ;
global main
main:
; access to the contents of a memory location
; requires square brackets around the address
;; any access to the address of a variable doesn't require brackets
; mov is
;; http://www.freebsd.org/doc/en/books/developers-handbook/x86-system-ca lls.html
;; Prompt user for input
mov eax, 4 ; moves sys_call write to eax
mov ebx, 1 ;moves std_out to ebx
mov ecx, msg1
mov edx, len1
int 0x80 ; could do 080h , calls kernel to write
;;
;;
;; Get user input
mov eax, 3 ;Moves sys_call read to eax
mov ebx, 0 ;Moves sys_call std_in to ebx
mov ecx, input1 ; address of buffer
mov edx, 255
int 0x80 ;length of buffer
;;
;;
;; Error checking
sub eax,1 ;remove carriage return
mov [length1], eax ;save length of string
;; Prompt user for input
mov eax, 4 ; moves sys_call write to eax
mov ebx, 1 ;moves std_out to ebx
mov ecx, msg2
mov edx, len2
int 0x80 ; could do 080h , calls kernel to write
;;
;;
;; Get user input
mov eax, 3 ;Moves sys_call read to eax
mov ebx, 0 ;Moves sys_call std_in to ebx
mov ecx, input2 ; address of buffer
mov edx, 255
int 0x80 ;length of buffer
;;
;;
;; Error checking
sub eax,1 ;remove carriage return
mov [length2], eax ;save length of string
loop:
mov esi, input1 ; point to start of string1
mov edi, input2 ; point to start of string2
mov eax,[length1] ; compare string sizes
mov ebx,[length2] ; and loop based on smallest
cmp eax,ebx ; string size
jg str2_Sml ; string2 is smaller jump
; string1 is smaller...
mov ecx, eax ; initialize count
jmp L1_test ; start the loop
str2_Sml:
mov ecx, ebx ; initialize count
jmp L1_test ; start the loop
L1_test:
cmp ecx,31 ; if there are more then 31 chars
jb L1_top
mov ecx,31 ; fail safe
L1_top:
mov al, [esi] ; get a character from str1
mov bl, [edi] ; get a character from str2
inc esi ; update str1 ptr
inc edi ; update str2 ptr
xor al, bl ; find the different bits
mov ah, 8 ; setup innerloop counter
L2_top:
dec ah ; decrement bit counter
jz L1_cont ; get new char to ham every
; 8 bits
shr al,1 ; rotate string1
jc Bit_on ; bit is on
jmp L2_top ; bit is off so re-loop
Bit_on:
; we have a different bit
inc dword [count] ; inc ham count
jmp L2_top ; cont loop
L1_cont:
dec ecx ; update char count
jnz L1_top ; loop to top if more chars
;trying to print
我认为这是我的错误所在:
mov eax, 4 ; moves sys_call write to eax
mov ebx, 1 ;moves std_out to ebx
mov ecx,ecx
mov edx, 255
int 0x80 ; calls kernel
exit:
mov eax, 1 ;exit
mov ebx, 0 ;
int 0x80 ;kernel command