我试图添加两个数组值并将其存储在下一个数组中

时间:2015-05-14 17:05:14

标签: assembly

下面的代码假设对名为“array”的数组进行排序。然后忽略它拥有的所有零,添加两个最低位,并将值存储到名为“parent”的新数组中。气泡排序可以准确地对阵列进行排序。但它没有按照它的设想进行排序。有人可以帮帮我吗?

    ;huffman seperation

    lea dx,PROMPT_1       ;moving the address of PROMPT_1 message
    call display          ;displays PROMPT_1 message
    lea si,array          ;moving the address of array in source index
    call PRINT_ARRAY      ;prints the whole array on screen
    lea si,array          ;moving the address of array in source index
    call BUBBLE_SORT      ;sorting algorithm to sort an array
    lea dx, PROMPT_2      ;load and display the string PROMPT_2
    call display
    lea si, array         ;set SI=offset address of ARRAY
    lea di, parent        ;loading an address of parent array in DI
    call PRINT_ARRAY      ;call the procedure PRINT_ARRAYT
    mov ax, 0             ;used for moving address of array during addition
    mov cx,512            ;512 times this calculation should be performed
    lea si,array          ;SI= pointer to array

huffloop:
calculation:
    mov bx,[si]           ;loading the address of array in bx
    cmp bx,'0'            ;if array has 0 value then jump to skipstep
    jnz skipstep
condition:
    add si+1,si           ;if condition satisfies then add first two array contents
    mov di,si+1           ;copying the resultant value to parent array too
    mov  [si],0           ;replacing the first lowest value in array to zero
    lea dx,PROMPT_1       ;displaying the result on screen
    call display
    lea si,array
    call PRINT_ARRAY
    lea si,array
    call BUBBLE_SORT      ;doing bubble sorting again for repeating the step
    LEA DX, PROMPT_2      ;load and display the string PROMPT_2
    call display
    lea SI, array         ;set SI=offset address of ARRAY
    call PRINT_ARRAY      ;call the procedure PRINT_ARRAY
    mov ax,0
skipstep:
    add si,2              ;moving to next array address
    jmp calculation       ;jumping to calculation function
    jnz huffloop          ;jumping to huffloop
endofhuff:

1 个答案:

答案 0 :(得分:0)

除了您的代码无法汇编之外,这是一个逻辑错误:

calculation:
    mov bx,[si]           ;loading the address of array in bx
    cmp bx,'0'            ;if array has 0 value then jump to skipstep
    jnz skipstep
condition:
    ...
skipstep:

jnz错了。只要数据不是 字符 skipstep,就会转到'0'分支,因此只有'0'才能处理环。测试应该是

    cmp bx,'0'            ;if array has 0 value then jump to skipstep
    jz skipstep