代码之家  ›  专栏  ›  技术社区  ›  idish

1到100之间的数字之和

  •  1
  • idish  · 技术社区  · 11 年前

    我是ASM的新手。 我试图完成一个简单的任务——1到100之间的数字的总和, eax 将持有总额。
    例如: 1 + 2 + 3 + .. + 100

    因此,以下是相关代码:

        XOR eax, eax ;; Set eax to 0
    MOV ecx, 100 ;; We will loop 100 times
    my_loop:
        ADD eax, ecx ;; We add the ecx register value to eax, ecx decreses by 1 every iteration untill he reaches 0
    LOOP my_loop
      ;;Exit the program, eax is the exit code
    push eax
    call ExitProcess
    

    当我调试exe文件时, eax公司 正在为0。这怎么可能?

    顺便说一句,有什么简单的方法可以打印 EAX 到控制台,而不是打开Windbg检查其值?

    3 回复  |  直到 11 年前
        1
  •  0
  •   johnfound    11 年前

    这个程序,有点适应 FreshLib 工作起来很有魅力。程序的核心是一样的,我只是添加了一些控制台输出。(好吧,这是FASM语法)所以,您只是没有注意到程序工作正常。

    include "%lib%/freshlib.inc"
    
    @BinaryType console
    
    include "%lib%/freshlib.asm"
    
    start:
            InitializeAll
    
            XOR  eax, eax ;; Set eax to 0
            MOV  ecx, 100 ;; We will loop 100 times
    my_loop:
            ADD  eax, ecx ;; We add the ecx register value to eax, ecx decreses by 1 every iteration untill he reaches 0
            LOOP my_loop
      ;;Exit the program, eax is the exit code
            mov  ebx, eax
    
            stdcall NumToStr, ebx, ntsDec or ntsSigned
            stdcall FileWriteString, [STDOUT], eax
    
            stdcall FileReadLine, [STDIN]  ; in order to pause until ENTER is pressed.
    
            stdcall TerminateAll, ebx 
    
    @AllDataEmbeded
    @AllImportEmbeded
    
        2
  •  0
  •   Jose    11 年前

    虽然我使用Irvine32.inc库打印我的结果,但这是可行的,只需使用您自己的方法打印即可。结果仍在EAX中

    TITLE   SOF_Sum
    
    INCLUDE Irvine32.inc ;may remove this and use your own thing
    
    .code
    
    MAIN PROC
        MOV EAX, 0 ; or XOR EAX, EAX - sets eax to 0
        MOV ECX, 100 ; loop counter - our loop will run 100 times
    
        myloop:
            ADD EAX, ECX ; adds ECX to EAX
        loop myloop
    
        call writedec ;displays decimal version of EAX, from Irvine32.inc, replace
    
    exit
    main ENDP
    END main
    

    我认为这里的重要部分是循环程序,其他部分可以由您自己设计。

    希望这能有所帮助(:

    JLL公司

        3
  •  -2
  •   Michael Petch    8 年前
    assume cs:code,ds:data
    data segment
    org 2000h
    series dw 1234h,2345h,0abcdh,103fh,5555h
    sum dw 00h
    carry dw 00h
    data ends
    code segment
    start:mov ax,data
    mov ds,ax
    mov ax,00h
    mov bx,00h
    mov cx,05h
    mov si,2000h
    go:add ax,[si]
    adc bx,00h
    inc si
    inc si
    dec cx
    jnz go
    mov sum,ax
    mov carry,bx
    mov ah,4ch
    int 21h
    code ends
    end start