Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions simple_asm/simple_atoi.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
;program untuk menconvert input dari user menjadi angka karena input yang di dapat
;berupa karakter ascii bukan angka
;atau program ini atoi pada bahasa c dengan versi saya sendiri
;algoritmanya adalah kali sepuluh hasil yang ditampung terus ditambah dengan digit baru


section .data
num db 0

Comment on lines +7 to +9

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

num db 0 cuma allocate 1 byte saja, tapi di syscall readnya specify buffer
length 3 bytes. Overflow write to memory!!!

Kenapa tidak pakai uninitialized storage saja di .bss?

Misal:

section .bss
	num resb 32

dan juga kenapa cuma 3 karakter doang readnya?

section .text
global _start

_start:
;code here
mov eax,3 ;sys_read
mov ebx,0 ;file desceiptor stdin

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xor ebx, ebx?

mov ecx,num
mov edx,3
int 0x80
Comment on lines +15 to +19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read 3 bytes, kamu masukin angka 2 karakter, lalu enter itu sudah 3 bytes.
Gimana kalau inputnya lebih dari 2 karakter? Hmmm?


mov edi,num

mov al, byte 0 ;hasil perkalian

Comment on lines +22 to +24

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xor eax, eax?

get_char:

movzx edx, byte[edi] ;mov one byte edi to edx / dl

cmp dl,48 ;jika kurang dari 0 end program
jl end

cmp dl,57 ;jika lebih dari 9 end program
jg end

mov bl,byte [edi]
sub bl, byte '0'
mov dl, byte 10
Comment on lines +26 to +37

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent spaces.


mul dl ;kali angka
add al,bl ;tambahkan hasil perkalian dengan digit input

inc edi ;char selanjutnya
jmp get_char
Comment on lines +25 to +43

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very wrong atoi().

Simple test cases that prove this wrong:

+-------+-------+
| Input | Hasil |
+-------+-------+
| 3333  | 773   |
| 4444  | 1884  |
| 5555  | 435   |
+-------+-------+



;hasil nya berada pada register eax tepatnya di al berukuran 1 byte
end:
mov eax,1
mov ebx,0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xor ebx, ebx?

int 0x80