Author Topic: help convert piece of code from c to assembly  (Read 11725 times)

nobody

  • Guest
help convert piece of code from c to assembly
« on: October 12, 2005, 05:21:35 AM »
for( k = -28; k < 25; k+= 8 )
    {
        printf( "k = %d\n", k - 5 );
    }


any help would be greatly appreciated.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: help convert piece of code from c to assembly
« Reply #1 on: October 12, 2005, 07:00:14 AM »
; nasm -f elf myprog.asm
; ld -s -o myprog myprog.o

global _start
section .text
_start:
    mov ebx, -28
top:
    lea eax, [ebx - 5]
    call showeaxsigned
    add ebx, 8
    cmp ebx, 25
    jl top
exit:
    mov eax, 1
    int 80h

;---------------------------
putc:
    push edx
    push ecx
    push ebx
    push eax

mov eax, 4
    mov ebx, 1
    mov ecx, esp
    mov edx, 1
    int 80h

pop eax
    pop ebx
    pop ecx
    pop edx
    ret
;-----------------------------    

;--------------------
showeaxsigned:
    or eax, eax
    jns positive
    push eax
    mov eax, '-'
    call putc
    pop eax
    neg eax
positive:
    call showeaxd
    mov al, 10
    call putc
    ret
;------------------------


;---------------------------------
showeaxd:
    push eax
    push ebx
    push ecx
    push edx
    push esi

sub esp, 10h
    lea ecx, [esp + 12]
    mov ebx, 10
    xor esi, esi
    mov byte [ecx], 0
.top:
    dec ecx
    xor edx, edx
    div ebx
    add dl, '0'
    mov [ecx], dl
    inc esi
    or eax, eax
    jnz .top

mov edx, esi
    mov ebx, 1
    mov eax, 4
    int 80h



add esp, 10h

pop esi
    pop edx
    pop ecx
    pop ebx
    pop eax

ret
;---------------------------------

Oh, you didn't want it for Linux? Shoulda left it in C! :)

Best,
Frank

nobody

  • Guest
Re: help convert piece of code from c to assembly
« Reply #2 on: October 13, 2005, 04:10:46 AM »
It is real interesting to see a student have you do his homework assignment.

nobody

  • Guest
Re: help convert piece of code from c to assembly
« Reply #3 on: October 13, 2005, 04:49:04 AM »
lol, *nods*