2011-11-09 29 views
0

我想找到預定義數組中的最大數字並將其輸出到屏幕。現在我知道一個事實,我的邏輯找到最大的數字是正確的,但輸出它就像打一場永不止息的戰爭!我創建了一個dd數組,但不能輸出任何東西

segment .data 


    matrix dd 1,62,3,44,35, \ 
      61,52,43,45,55, \ 
      17,23,37,74,65, \ 
      13,12,93,94,95, \ 
      31,21,13,14,25 \ 


segment .bss 

holder resb 4 

counter resb 4 


segment .text 

global _start 

_start: 

    mov eax, matrix 
    call big 

big: 
    mov esi, holder 
    mov edi, counter 
    mov edi, 0 
    jmp switch 

loop: 
    inc edi 
    cmp esi, [eax + edi] 
    jg switch 
    cmp edi, 25 
    jle loop 
    mov eax, [esi] 
    add eax, '0' 

    mov eax, 4 ; after some advice from a few forum member i tried the [ebx + ecx *4] but no luck 
    mov ebx, 1 
    mov ecx, eax 
    mov edx 
    mov eax, [ebx + ecx * 4] 

    int 0x80 


switch: 
    mov esi, [eax + edi] 
    jmp loop 


exit: 
    mov eax, 1 
    xor ebx, ebx 
    int 0x80 
+0

介意告訴我們有關您使用的平臺和操作系統的任何信息?打印輸出是您可以想象的最具平臺特色的事情之一。 –

+0

@KerrekSB SB哇這有趣我不知道我認爲這是通用語言,即時通訊使用Linux和nasm編譯器進行彙編 –

+0

它如何通用?你可以用匯編語言編寫「裸機」代碼(例如bootloader),或者你可以編寫一個「託管程序」,任何和所有的細節都依賴於操作系統......在託管環境中,你通常想要調用一些適當的系統調用。無論如何,「Linux」是這裏重要的信息。乾杯。 –

回答

0

我知道這並不能回答你的問題,但我想你可能想知道如何找到在列表中數量最多的一個更有效的方式:

mov  esi, matrix  ; esi now points to the beginning of the matrix 
xor  ebx, ebx   ; ebx will mold the max 
xor  ecx, ecx   ; ecx is the counter 

loop: 
    cmp ecx, 25   ; Make sure the end of the matrix has not been reached 
    jge end_loop   ; If the end has been reached, jump out of the loop 

    mov eax, [esi+ecx*4] ; Read the next DWORD from the matrix 
    cmp ebx, eax   ; Compare it to ebx (the current max) 
    jle skip    ; If it's not greater than the current max, skip it 
    mov ebx, eax   ; Otherwise, update ebx with the new max 

    skip: 
    add ecx, 1   ; incriment the counter 
jmp  loop    ; Loop to the end of the matrix 

end_loop: 

; ebx now contains the max value in the 25 number matrix 
相關問題