Skip to content
Advertisement

Loop Never Ends in Assembly?

Basically, I have below assembly code:

global _start
section .bss
    input:  resq 100
section .data
    buf: db "Input> ",  7
    pas: db "password", 8
section .text
_start:
    mov eax, 4
    mov ecx, buf
    mov edx, 7
    int 0x80

    mov eax, 3
    mov ecx, input
    mov edx, 100
    int 0x80
    mov cx, 50
Loop:
    dec cx
    mov eax, 4
    mov ecx, pas
    mov edx, 8
    int 0x80

    jnz Loop
    mov eax, 1
    int 0x80

I am trying to loop through 0-50 and print the variable pas or probably buf from input box. But this loop keeps printing pas endlessly. I Presume the issue is the re-assignment of ecx, but otherwise without putting my string into ecx how would I print it? or even execute any thing within my loop at all?

Arc: Linux kali 5.2.0-kali2-amd64 #1 SMP Debian 5.2.9-2kali1 (2019-08-22) x86_64 GNU/Linux

Advertisement

Answer

My assumption by overwriting ecx was correct, fixed it by using push & pop as below:

Credit goes to @Jester.

global _start
section .bss
    input:  resq 100
section .data
    buf: db "Input> ",  7
    pas: db "password", 8
section .text
_start:
    mov eax, 4
    mov ecx, buf
    mov edx, 7
    int 0x80

    mov eax, 3
    mov ecx, input
    mov edx, 100
    int 0x80
    xor cx, cx

Loop:
    inc cx
    push cx
    mov eax, 4
    mov ecx, input
    mov edx, 8
    int 0x80

    pop cx
    cmp cx, 5
    jne Loop

    mov eax, 1
    int 0x80
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement