1到100之间的数字之和

时间:2013-10-14 00:48:26

标签: assembly x86 windbg masm masm32

我是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 个答案:

答案 0 :(得分:0)

这样可行,虽然我使用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

答案 1 :(得分:0)

这个有点适合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 :(得分:-2)

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
相关问题