1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[assembly] Writing a simple i/o program

  1. Oct 15, 2012 #1
    1. The problem statement, all variables and given/known data

    I have to write a simple program in NASM to read in a character then output what kind of character it is.


    3. The attempt at a solution

    There are some things about NASM that confuse me so i marked them.

    Code (Text):
    segment .data

        BUFFLEN equ 1 ; length of buffer
        CtrlMsg db "Control Character", 10 ;what does this 10 do?
        SymbolMsg db "Symbol", 10
        NumberMsg db "Number", 10
        CapitalMsg db "Capital Letter", 10
        LowercaseMsg db "Lower Case Letter", 10

    segment .bss

        input resb 1 ; characters are 1 byte

    segment .text

        global _start

    _start: ; main function

        mov eax, 3
        mov ebx, 1
        mov ecx, input
        mov edx, BUFFLEN
        int 0x80 ; interrupt and get user input
       
        cmp byte [ecx],20h ; go through the ascii table ranges
        jb IsControl
        cmp byte [ecx],30h
        jb IsSymbol
        cmp byte [ecx],3Ah
        jb IsNumber
        cmp byte [ecx],41h
        jb IsSymbol
        cmp byte [ecx],5Bh
        jb IsCapital
        cmp byte [ecx],61h
        jb IsSymbol
        cmp byte [ecx],7Bh
        jb IsLowercase
        cmp byte [ecx],7Fh
        je IsControl
       
        jmp IsSymbol ; for anything else, assume it's a symbol
       
    IsLowercase:

        mov edx, len    ; how do you get the length of a string in nasm?    
        mov ecx, LowercaseMsg
        jmp Print  

    IsCapital:

        mov edx, len           
        mov ecx, CapitalMsg
        jmp Print
       
    IsSymbol:

        mov edx, len           
        mov ecx, SymbolMsg
        jmp Print
       
    IsNumber:

        mov edx, len         
        mov ecx, NumberMsg  
        jmp Print
       
    IsControl:

        mov edx, len         
        mov ecx, CtrlMsg
        jmp Print
       
    Print:

        mov ebx, 1      ; writing to screen
        mov eax, 4      ; sysout command
        int 0x80        ; interrupt
       
    exit:

        mov eax, 1 ; Select system call 1
        xor ebx, ebx ; Set default exit code
        int 0x80 ; Invoke selected system call
     
  2. jcsd
  3. Oct 15, 2012 #2

    rcgldr

    User Avatar
    Homework Helper

    What type of operating system is this running on? The '10' on the strings is an ASCII line feed.

    If you want to generate a symbol for the length of a string, you can use an equate, in MASM, where $ means the current location, this would be:

    Code (Text):

    CtrlMsg    db     "Control Character", 10
    CtrlMsgLen equ    $ - offset CtrlMsg
     
     
  4. Oct 15, 2012 #3
    I'm doing all my debugging etc on Linux.

    Is the '10' needed at all or is it just there to make the output pretty?

    Code (Text):

        CtrlMsg db "Control Character", 10
        CtrlMsgLen equ $ - CtrlMsg
        SymbolMsg db "Symbol", 10
        SymbolMsgLen equ $ - SymbolMsg
        NumberMsg db "Number", 10
        NumberMsgLen equ $ - NumberMsg
        CapitalMsg db "Capital Letter", 10
        CapitalMsgLen equ $ - CapitalMsg
        LowercaseMsg db "Lower Case Letter", 10
        LowercaseMsgLen equ $ - LowercaseMsg
     
  5. Oct 15, 2012 #4

    rcgldr

    User Avatar
    Homework Helper

    If you output multiple messages, they'll all end up connected as one message on the screen if you don't have the '10'. For Windows / Dos, you need a carriage return and a line feed (13, 10). Usually control characters are written using hex notation, so line feed would be 00ah.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [assembly] Writing a simple i/o program
  1. Program Trace [assembly] (Replies: 25)

Loading...