Skip to main content
Engineering LibreTexts

3.3: Prompting for an Input Integer Number

  • Page ID
    83044
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The next program, printInt, shows how to use scanf to read an input integer number from a user and then how to output that number back to the user using printf. It is contained in the following example program.

    #
    # Program Name:   printIntMain
    # Author: Charles Kann
    # Date:     9/19/2020
    # Purpose: Uses scanf for an integer using data memory
    # Input:
    #   - input: User entered number
    # Output:
    #   - format: Prints the number
    #
    
    .text
    .global main
    
    main:
        # Save return to os on stack
        SUB sp, sp, #4
        STR lr, [sp, #0]    
        
        # Prompt for an input
        LDR r0, =prompt
        BL  printf    
        
        # Scanf
        LDR r0, =input
        LDR r1, =num
        BL scanf    
        
        # Printing the message
        LDR r0, =format
        LDR r1, =num
        LDR r1, [r1, #0]
        BL printf    
        
        # Return to the OS
        LDR lr, [sp, #0]
        ADD sp, sp, #4
        MOV pc, lr
    
    .data
        # Allocates space for a word-aligned 4-byte value in the memory
        num: .word 0
        # Prompt the user to enter a number
        prompt: .asciz "Enter A Number\n"
        # Format of the user input, %d means integer number
        input: .asciz "%d"
        # Format to print the entered number
        format: .asciz "Your Number Is %d \n"
    

    5 printIntMain

    all: helloWorld printName printInt
    helloWorld: helloWorldMain.s
          gcc $@Main.s -g -o $@      
          ./helloWorld
    printName: printNameMain.s      
          gcc $@Main.s -g -o $@      
          ./printName
    printInt:      
          gcc $@Main.s -g -o $@
          ./printInt

    6 makefile for printInt


    3.3: Prompting for an Input Integer Number is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?