Question

I'm a newbish in ASM. I'm trying to accomplish a simple task - sum of the numbers between 1 to 100, eax will hold the sum.
e.g: 1 + 2 + 3 + .. + 100

So here's the relevant code:

    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

When I debug the exe file, eax is being 0. How is it possible?

BTW, is there any easy way to print the value of EAX to the console, instead of opening Windbg to check its value?

Was it helpful?

Solution

This program, a little bit adapted for FreshLib works like a charm. The core of the program is the same, I simply added some console output. (and well, it is FASM syntax) So, you simply missed to notice that the program is working properly.

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

OTHER TIPS

This works, though I am using Irvine32.inc library to print my result, just use your own method to print. The result is still in 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

I think the important part here is the loop procedure, the others can be of your own design.

Hopefully this helps (:

JLL

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top