0

I am writing a simple assembly language program (32bit) on NASM/linux. The goal is to input two single digit numbers, compare them and print the first number if it is greater. Here is the code:

SECTION .bss
    num1 resb 1
    num2 resb 1

SECTION .DATA
    msg:    dd "Enter a Number:", 0x9 
    len:    equ $-msg
    msg2:   dd "Enter the second number:",0x9 
    len2:   equ $-msg2
   
    
SECTION .TEXT
    global _start
    
_start:
    ;print opening message
    mov edx,len;message length in bytes to edx
    mov ecx,msg;message body to ecx
    mov ebx,1;file descriptor (stdout)
    mov eax,4;sys call (sys_write)
    int 0x80
    
    ;read the first number
    mov edx, 1 ;length in bytes
    mov ecx, num1 ;storage address
    mov ebx,0 ;file descriptor -stdin
    mov eax,3 ;sys call - sys_read
    int 0x80 

    ;print second prompt
    mov edx,len2;message length in bytes to edx
    mov ecx,msg2;message body to ecx
    mov ebx,1;file descriptor (stdout)
    mov eax,4;sys call (sys_write)
    int 0x80

    ;read the second number
    mov edx, 1 ;length in bytes
    mov ecx, num2 ;storage address
    mov ebx,2 ;file descriptor -stdin
    mov eax,3 ;sys call - sys_read
    int 0x80 
    
    ;compare the two numbers
    mov eax,0
    mov ax,[num1]
    mov ecx,0
    mov cx,[num2]
    sub ax,'0'
    sub cx,'0'
    cmp eax,ecx
    jg greater

    greater:
    mov edx,1
    mov ecx,num1
    mov ebx,1
    mov eax,4
    int 0x80
    jmp end;

    end:

    mov eax,1
    int 0x80

The issue is that only the first input is accepted, after the second prompt is displayed, it skips the second input and only prints the first digit. Is this possible to do without using any functions or C code, and if so, how?

Ian K
  • 77
  • 4
  • Use `strace ./a.out` to debug your program, and notice that the second `read` system-call gets the newline from when you pressed enter the first time. Also single-step your program in a debugger and notice that loading two bytes from `num1` get both input characters. You want `movzx` to load a byte into a 32-bit register. – Peter Cordes Apr 25 '22 at 10:24

0 Answers0