1. Limited time only! Sign up for a free 30min personal 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!

Homework Help: [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

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


        mov edx, len           
        mov ecx, CapitalMsg
        jmp Print

        mov edx, len           
        mov ecx, SymbolMsg
        jmp Print

        mov edx, len         
        mov ecx, NumberMsg  
        jmp Print

        mov edx, len         
        mov ecx, CtrlMsg
        jmp Print

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

        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


    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


    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook