Enumero
Enumero

Reputation: 1

Problem with program in assembly x86 AT&T, division by repeated subtraction algorithm

I have a problem with my program. It should read from STDIN 32 bytes as divident, then next 32 bytes as divisor, after that it should divide divident by divisor using "division by repeated subtraction" algorithm and at last using STDOUT write the result(quotient and rest).

I have wrote a code in assembly x86 AT&T that is working to certain moment, sometimes without a change, only new assembling and linking, it stops working, program never read data to the end. After working correctly at the begging, later it provide wrong results.

This is my code:

EXIT_NR  = 1
READ_NR  = 3
WRITE_NR = 4

STDOUT = 1
STDIN = 0

EXIT_CODE_SUCCESS = 0

.data
dividend: .space 32
divisior: .space 32
quotient: .space 32
rest: .space 32

.text
.global _start
_start:
read:
movl $READ_NR, %eax
movl $STDIN, %ebx
movl $dividend,%ecx
movl $64, %edx
int $0x80

clc
cmp $0,%eax
jle end

movl $31,%esi
div_loop:
    clc
    cmp $0,%esi 
    je done
    movb dividend(%esi), %al 
    movb divisior(%esi),%bl
    xor %dl,%dl 
    sub_loop:
        sbbb %bl,%al
        jc odtw
        inc %dl
        jmp sub_loop
    odtw:
        movb %dl,result(%esi)
        addb %bl,%al
        movb %al,rest(%esi)
        dec %esi
        jmp div_loop
done:

movl $WRITE_NR,%eax
movl $STDOUT,%ebx
movl $result, %ecx
movl $64, %edx
int $0x80

jmp read

end:
movl $EXIT_NR, %eax
movl $EXIT_CODE_SUCCESS, %ebx
int $0x80

Hexdump of data and result(example):

00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 0b  |................|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 02  |................|
result:
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 05  |................|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 01  |................|

Upvotes: 0

Views: 83

Answers (0)

Related Questions