0
SYS_EXIT  equ 1  
SYS_READ  equ 3  
SYS_WRITE equ 4  
STDIN     equ 0  
STDOUT    equ 1  

segment .data

    msg1 db "Enter two digits: ", 0xA,0xD 
    len1 equ $- msg1

    msg2 db "Result: ", 0xA,0xD 
    len2 equ $- msg2

segment .bss

    num1 resb 1
    num2 resb 1
    res  resb 1

segment .text
    global _start
_start:
    ; disp msg
    mov   eax, SYS_WRITE         
    mov   ebx, STDOUT         
    mov   ecx, msg1         
    mov   edx, len1
    int   0x80

    ; enter num1
    mov   eax, SYS_READ
    mov   ebx, STDIN
    mov   ecx, num1
    mov   edx, 2   
    int   0x80

    ; print num1
    mov   eax, SYS_WRITE         
    mov   ebx, STDOUT         
    mov   ecx, num1         
    mov   edx, 2
    int   0x80

    ; enter num2
    mov   eax, SYS_READ
    mov   ebx, STDIN
    mov   ecx, num2
    mov   edx, 2
    int   0x80

    ; print num2
    mov   eax, SYS_WRITE         
    mov   ebx, STDOUT         
    mov   ecx, num2         
    mov   edx, 2
    int   0x80

    ; move first to eax, second to ebx
    mov   eax, num1
    sub   eax, byte '0'
    mov   ebx, num2
    sub   ebx, byte '0'

    ; or them and store the result in eax, and then eax in res
    or    eax, ebx
    add   eax, byte '0'
    mov   [res], eax

    ; disp result msg
    mov   eax, SYS_WRITE         
    mov   ebx, STDOUT         
    mov   ecx, msg2        
    mov   edx, len2
    int   0x80

    ; write result
    mov   eax, SYS_WRITE         
    mov   ebx, STDOUT         
    mov   ecx, res        
    mov   edx, 1
    int   0x80
outprog:
    mov    eax,1             ;system call number (sys_exit)
    int    0x80              ;call kernel

In the segment of code above, I wish to take two digits as input, or them, and display the result. However, when I compile and run this code, no matter which input I give the program, the program always return the character ]. I am fairly new to programming in assembly and I am not entirely sure as to why this is happening. I've done a bit of research and found that the ascii values for ] are (in order dec, hex): 93, 5D, however, I do not know what correlation that character has with the code I have written. Any explanation as to why this is happening and advice as to how to avoid such bugs in the future would be wonderful.

nrz
  • 10,435
  • 4
  • 39
  • 71
swarajd
  • 977
  • 1
  • 10
  • 18
  • You can't use `resb 1` for something that you intend to hold more than 1 byte (like `num1`, `num2` and `res`). – Michael Dec 10 '13 at 16:41
  • I do intend to have them hold one byte, since I want them to be one character/digit. – swarajd Dec 10 '13 at 16:45
  • Well, your code says otherwise. For example: `mov [res], eax` – Michael Dec 10 '13 at 16:49
  • True. Would changing `resb 1` to `resb 32` make the code somewhat better? Also, would that change the output that I get from the character ']'? – swarajd Dec 10 '13 at 16:52
  • `resb 4` or `resd 1` would be enough for a dword. I really don't know if that change alone would make your program work. That was just the most obvious thing that stuck out to me. – Michael Dec 10 '13 at 16:58
  • That change did change the output, but now it's the character 'l' (dec: 108, hex: 6C) – swarajd Dec 10 '13 at 17:22

1 Answers1

0

[PROBLEM SOLVED] I had to change mov eax, num1 to mov eax, [num1]

swarajd
  • 977
  • 1
  • 10
  • 18