Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a program in assembly that reads 2 integers from the user and 1 character

ID: 3566409 • Letter: C

Question

Create a program in assembly that reads 2 integers from the user and 1 character. We will call them a , b and op . The op will be a character and will be one of the following + (add), * (multiply), ^ (exponent), E (exit). You must write a function for each of the operators and you must store the three inputs in global variables (resb, resw, read, db...)

Helpful hints. Use printf. For the op, use two read chars (one of them will remove the extra newline). Put the printf in the main function and return the result of each function in the register eax (or a global variable).

call read_char

call read_char

mov [op], al

EXAMPLES:

user enters 4, 7 and + result is 4 + 7 = 11

user enters 6, 3 and ^ result is 6 ^ 3 = 216

Explanation / Answer

section .data
    str: times 100 db 0    ; to allocate 100 bytes with the value 0
section .bss

section .text

global _start

section .data
    str: times 100 db 0 ; Allocate buffer of 100 bytes
    lf: db 10          ; LF for full str-buffer

section .bss
    e1_len resd 1
    dummy resd 1

section .text

_start:
    mov eax, 3          ; Read user input into str
    mov ebx, 0          ; |
    mov ecx, str        ; | <- destination
    mov edx, 100        ; | <- length
    int 80h             ;

    mov [e1_len],eax    ; Store number of inputted bytes
    cmp eax, edx        ; all bytes read?
    jb .2               ; yes: ok
    mov bl,[ecx+eax-1] ; BL = last byte in buffer
    cmp bl,10           ; LF in buffer?
    je .2               ; yes: ok
    inc DWORD [e1_len] ; no: length++ (include 'lf')

    .1:                 ; Loop
    mov eax,3           ; SYS_READ
    mov ebx, 0          ; EBX=0: STDIN
    mov ecx, dummy      ; pointer to a temporary buffer
    mov edx, 1          ; read one byte
    int 0x80            ; syscall
    test eax, eax       ; EOF?
    jz .2               ; yes: ok
    mov al,[dummy]      ; AL = character
    cmp al, 10          ; character = LF ?
    jne .1              ; no -> next character
    .2:                 ; end of loop

    mov eax, 4          ; Print 100 bytes starting from str
    mov ebx, 1          ; |
    mov ecx, str        ; | <- source
    mov edx, [e1_len]   ; | <- length
    int 80h             ;

    mov eax, 1          ; Return
    mov ebx, 0          ; | <- return code
    int 80h             ;

Thank You
Please Rate

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote