Ads

Friday 11 October 2013

Microprocessor Architecture assignment no 21 ASCII

; display characters and decimal ascii codes from a file
;Assignment no.:
;Name:
;Roll no.:

SECTION .data
    file_name db 'abc.txt'

SECTION .bss
    fd_in resd 1
    filecontent resb 250
    fbuff_len equ $-filecontent
    length_read resd 1


SECTION .text
global _start
_start:
    mov eax,5 ;open a file
    mov ebx,file_name
    mov ecx,0
    mov edx,777
    int 80h

; did open succeed?
    test eax, eax
    js exit


    mov dword[fd_in],eax

    mov eax,3 ;read from file
    mov ebx,[fd_in]
    mov ecx,filecontent
    mov edx,fbuff_len
    int 80h
; eax has length actually read
    mov [length_read], eax

    mov esi,filecontent

; point edi to end of file
    mov edi, esi
    add edi, [length_read]

    xor eax, eax ; just to make sure upper bytes are clear
nextnum:
    mov al,byte[esi]

dispnum:
    call showeaxd
    ;mov al, 10 ; linefeed
    ;call putc

    inc esi
    cmp esi, edi
jnz nextnum

    mov eax,6 ; __NR_close
    mov ebx,[fd_in]
    int 80h

; pretend no error
    xor eax, eax

exit:
    mov ebx, eax ; possible error number in ebx
    neg ebx  ; negate it for easier reading with "echo $?"
    mov eax,1
    int 80h

;---------------------------
; print character in al
putc:
    push edx
    push ecx
    push ebx
    push eax ;this serves as our buffer

    mov ecx, esp ; buffer
    mov edx, 1 ; just 1
    mov ebx, 1 ; stdout
    mov eax, 4 ; __NR_write
    int 80h

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

;---------------------------------
; showeaxd - print a decimal representation of eax to stdout
; for "debug purposes only"... mostly
; expects: number in eax
; returns; nothing useful
showeaxd:
    push eax
    push ebx
    push ecx
    push edx
    push esi
 
    sub esp, 10h
    lea ecx, [esp + 12]
    mov ebx, 10
    xor esi, esi
 
.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
;---------------------------------


OUPUT:

[pict@localhost ~]$ nasm -f elf asgn21.asm
[pict@localhost ~]$ ld -o asgn21 asgn21.o
[pict@localhost ~]$ ./asgn21
9710
[pict@localhost ~]$

No comments:

Post a Comment