hochesh's blog

By hochesh, history, 2 years ago, In Russian

HELp!!!!!

I cann't find a BUG in my CODE.

.text
.global main

main: call readi32 movl %eax, %ebx // ebx = size movl %esp, %ecx // ecx = initial stack

read_seq: // read sequence and check it testl %ebx, %ebx jz finish_read decl %ebx

call    readi32
cmpl    $0, %eax
jl   negative

pushl   %eax
jmp     read_seq

negative: cmpl %esp, %ecx je wrong_sequence popl %edx addl %eax, %edx jnz wrong_sequence jmp read_seq

wrong_sequence: xorl %eax, %eax call writei32 call finish

finish_read: cmpl %esp, %ecx jne wrong_sequence

movl    $1, %eax
call    writei32
call    finish

And this must be compiled with simpleio lib:

/* -*- asm -*- */

.text

_form1: .asciz "%d" .text .align 16 .global readi32 readi32: pushl %ebp movl %esp, %ebp

subl    $4, %esp
    and     $-16, %esp
    leal    -4(%ebp), %eax

    push    %ecx
    push    %edx

    sub     $8, %esp
    mov     %eax, 4(%esp)
    movl    $_form1, (%esp)

ifdef APPLE

call    _scanf

else

call    scanf

endif

add     $8, %esp
    movl    %eax, %ecx
    movl    -4(%ebp), %eax
    clc
    cmpl    $1, %ecx
    je      readi32x
    stc

readi32x:
pop %edx pop %ecx movl %ebp, %esp popl %ebp ret

.text

_form2: .asciz "%d\n" .text .align 16 .global writei32 writei32:
pushl %ebp movl %esp, %ebp and $-16, %esp push %ecx push %edx

sub     $8, %esp
    mov     %eax, 4(%esp)
    movl    $_form2, (%esp)

ifdef APPLE

call    _printf

else

call    printf

endif

add     $4, %esp

    push stdout
    call fflush
    add $4, %esp

    pop     %eax
    pop     %edx
    pop     %ecx

    movl    %ebp, %esp
    popl    %ebp
    ret

    .align  16
    .global nl

nl: pushl %ebp movl %esp, %ebp and $-16, %esp push %ecx push %edx

sub     $8, %esp
    movl    $'\n', (%esp)

ifdef APPLE

call    _putchar

else

call    putchar

endif

add     $8, %esp
    pop     %edx
    pop     %ecx
    movl    %ebp, %esp
    popl    %ebp
    ret

    .text
    .align  16
    .global finish

finish: and $-16, %esp sub $16, %esp movl $0, (%esp)

ifdef APPLE

call    _exit

else

call    exit

endif

.text

_form3: .asciz "%lli" .align 16 .global readi64 readi64: pushl %ebp movl %esp, %ebp sub $8, %esp and $-16, %esp push %ecx

leal    -8(%ebp), %eax
    sub     $12, %esp
    mov     %eax, 4(%esp)
    movl    $_form3, (%esp)

ifdef APPLE

call    _scanf

else

call    scanf

endif

add     $12, %esp
    movl    %eax, %ecx
    movl    -8(%ebp), %eax
    movl    -4(%ebp), %edx
    clc
    cmpl    $1, %ecx
    je      readi64x
    stc

readi64x:
pop %ecx movl %ebp, %esp popl %ebp ret

.text

_form4: .asciz "%lld\n" .align 16 .global writei64 writei64:
pushl %ebp movl %esp, %ebp and $-16, %esp push %ecx

sub     $12, %esp
    mov     %edx, 8(%esp)
    mov     %eax, 4(%esp)
    movl    $_form4, (%esp)

ifdef APPLE

call    _printf

else

call    printf

endif

add     $4, %esp

    push stdout
    call fflush
    add $4, %esp

    pop     %eax
    pop     %edx
    pop     %ecx

    movl    %ebp, %esp
    popl    %ebp
    ret
  • Vote: I like it
  • +29
  • Vote: I do not like it

| Write comment?
»
2 years ago, # |
  Vote: I like it +26 Vote: I do not like it

Why codeforces does not support assembly language?