0

I'm trying to sum two one digit numbers, but when the answer is greater than 10 it gives me an error "<" (unsure what this means), it works fine if the answer is below 10, ex 4 3 = 7. I've gone through my code, and I can't find any errors, is it something i forgot to add? Any help is appriciated :)!

Assembly code:

; Konstanter
  cr equ 13 ; Vognretur
  lf equ 10 ; Linjeskift
  SYS_EXIT  equ 1
  SYS_READ  equ 3
  SYS_WRITE equ 4
  STDIN     equ 0
  STDOUT    equ 1
  STDERR    equ 2

; Datasegment
section .bss
  siffer resb 4
  
; Datasegment
section .data
  meld db "Skriv to ensifrede tall skilt med mellomrom.",cr,lf
    db "Summen av tallene maa vaere mindre enn 10.",cr,lf
  meldlen equ $ - meld
  feilmeld db cr,lf, "Skriv kun sifre!",cr,lf
  feillen equ $ - feilmeld
  crlf db cr,lf
  crlflen equ $ - crlf
  
; Kodesegment med program
section .text

global _start
_start:
  mov edx,meldlen
  mov ecx,meld
  mov ebx,STDOUT
  mov eax,SYS_WRITE
  int 80h
  
  call lessiffer
  cmp edx,0 
  jne Slutt 
  mov eax,ecx 
  
  call lessiffer
  ; vellykket: edx=0, tall i ecx
  cmp edx,0 
  jne Slutt
  mov ebx,ecx 
  
  call nylinje
  add eax,ebx
  mov ecx,eax
  call skrivsiffer 
  
Slutt:
  mov eax,SYS_EXIT
  mov ebx,0
  int 80h

; ---------------------------------------------------------
skrivsiffer:
  ; Skriver ut sifferet lagret i ecx. Ingen sjekk på verdiområde.
  push eax
  push ebx
  push ecx
  push edx
  add ecx,'0'
  mov [siffer],ecx
  mov ecx,siffer
  mov edx,1
  mov ebx,STDOUT
  mov eax,SYS_WRITE
  int 80h
  pop edx
  pop ecx
  pop ebx
  pop eax
  ret
  
; ---------------------------------------------------------
lessiffer:
  push eax
  push ebx
  
Lokke:
  ; Leser et tegn fra tastaturet
  mov eax,SYS_READ
  mov ebx,STDIN
  mov ecx,siffer
  mov edx,1
  int 80h
  mov ecx,[siffer]
  cmp ecx,' '
  je Lokke
  cmp ecx,'0'
  jb Feil
  cmp ecx,'18'
  ja Feil
  sub ecx,'0'
  mov edx,0 
  pop ebx
  pop eax
  ret 
  
Feil:
  mov edx,feillen
  mov ecx,feilmeld
  mov ebx,STDERR
  mov eax,SYS_WRITE
  int 80h
  mov edx,1 
  pop ebx
  pop eax
  ret 
  
; ---------------------------------------------------------
; Flytt cursor helt til venstre på neste linje
nylinje:
  push eax
  push ebx
  push ecx
  push edx
  mov edx,crlflen
  mov ecx,crlf
  mov ebx,STDOUT
  mov eax,SYS_WRITE
  int 80h
  pop edx
  pop ecx
  pop ebx
  pop eax
  ret
  
; End _start
fuz
  • 76,641
  • 24
  • 165
  • 316
Emmyx97
  • 13
  • 2
  • 2
    Your code can only print one digit numbers. If you try to print a multi-digit number, it'll print the characters that follow `9` in the ASCII code table. If you want to be able to print multi-digit numbers, you need to write a routine to convert your numbers to decimal. – fuz Oct 11 '20 at 17:39
  • Do you know where I can find a good tutorial on this? I can only find tutorials that show me how to get what i already have – Emmyx97 Oct 11 '20 at 18:28
  • Let me find an appropriate duplicate. – fuz Oct 11 '20 at 18:54
  • Please also refer to the [x86] tag wiki for more resources. – fuz Oct 11 '20 at 18:56

0 Answers0