Author Topic: add(sum) store them in one register and count into another eventNumb only  (Read 10403 times)

Offline new_geek

  • Jr. Member
  • *
  • Posts: 11
  • Country: 00
Hi again, this time I got something in my mind. Its about making a file out of fun. Supposed there is an array of 15 numbers, let say in 8bit signed integers. How do we sum them up one by one by taking even numbers only and then we count up how many numbers are they. The easiest test, would be we give the range, let say 1 - 15 is the number. So we ended up having 5 of them which are 1,3,5,7,9,11,13,15. and that would give a sum equal to 64.

Quote
Code: [Select]
section .data
list db 1,2,3,4,5,6,7,8,9,10
evenAdd db 0

section .bss

section .text
global _start

_start:
nop

; TODO: your code here

;clearing the registers that are going to be used
mov ax,0
mov bx,0
mov cx,0
mov dx,[evenAdd]

initialLoop:
mov bx,[list]
mov cx,1[b]
cmp bx,00
je dataCount[/b]

dataCount:
add dx,cx
jmp countContinue

countContinue:
add ax,bx
add cx,1
cmp ax,0
je exit
jmp initialData

exit:
mov ebx,0
mov ebx,eax
mov eax, 1
int 0x80

I found that code in bold supposed to compare the numbers and get the even number. but its not working the number keep in loop and won't stop...
« Last Edit: October 29, 2013, 02:48:43 AM by new_geek »

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
Hello!

This is my 32 bit number version and works too.
It add's even values into memory address dword [evenAdd].
It count's even values into memory address dword [evenCount].

LINUX:

Code: [Select]
section .data
list: dd 1,2,3,4,5,6,7,8,9,10
evenAdd: dd 0
evenCount: dd 0

section .bss

section .text
global _start

_start:
nop

; TODO: your code here

;clearing the registers that are going to be used
xor ecx,ecx
mov ebx,dword 2
mov esi,dword list

initialLoop:
push ecx ; (+1)
xor eax,eax
mov eax,dword [esi]
mov ecx,eax
div ebx
cmp edx,dword 1
je initialLoop.thisIsOddNumber
add dword [evenAdd],ecx
inc dword [evenCount]
initialLoop.thisIsOddNumber:
pop ecx ; (-1)
inc ecx
add esi,dword 4
cmp ecx,dword 10
jl initialLoop

; call CrackMonitorScreen

mov ebx,0
mov ebx,eax
mov eax, 1
int 0x80

; If data is => list: dd 1,2,3,4,5,6,7,8,9,10
; Then loop named "initialLoop", produces:
; value of dword [evenAdd] => 30
; value of dword [evenCount] => 5

WINDOWS:
This actually is the version, that i tested:

Code: [Select]
; ---------------------------------------------------------------------------
; WINDOWS VERSION
; ---------------------------------------------------------------------------

; ---------------------------------------------------------------------------
; Tell compiler to generate 32 bit code
; ---------------------------------------------------------------------------
bits 32

; ---------------------------------------------------------------------------
; Data segment:
; ---------------------------------------------------------------------------
section .data use32

        ; Multiline printf format
        txt_format: db 10,"==========================="
                        db 10,"Hello world:"
                        db 10,"==========================="
                        db 10,"evenAdd: %d"
                        db 10,"evenCount: %d"
                        db 10,"===========================",10,0
        align 8 ; Align data
list: dd 1,2,3,4,5,6,7,8,9,10
        align 8 ; Align data
evenAdd: dd 0
        align 8 ; Align data
evenCount: dd 0
        align 8 ; Align data

; ---------------------------------------------------------------------------
; Code segment:
; ---------------------------------------------------------------------------
section .text use32


        ; ---------------------------------------------------------------------------
        ; C management
        ; ---------------------------------------------------------------------------
        global main
        extern printf

main:


xor ecx,ecx
mov ebx,dword 2
mov esi,dword list

initialLoop:
push ecx ; (+1)
xor eax,eax
mov eax,dword [esi]
mov ecx,eax
div ebx
cmp edx,dword 1
je initialLoop.thisIsOddNumber
add dword [evenAdd],ecx
inc dword [evenCount]
initialLoop.thisIsOddNumber:
pop ecx ; (-1)
inc ecx
add esi,dword 4
cmp ecx,dword 10
jl initialLoop


push dword [evenCount]
push dword [evenAdd]
push dword txt_format
call printf
add esp,dword 4*3

        ; -----------------------------------------------------------------------------
        ; Quit
        ; -----------------------------------------------------------------------------
        mov eax,dword 0
        ret

; ----
; END ----
; ----

This is output of windows example:
Code: [Select]
===========================
Hello world:
===========================
evenAdd: 30
evenCount: 5
===========================

I compiled like this:
"nasm.exe test.asm -f win32 -o test.obj"
"golink.exe /entry main test.obj MSVCRT.dll"

And that's it!
Encryptor256!!!

Edit:
Attachment contains updated code of Windows and Linux versions.
Attachment, Version History:
1. WindowsVersion1.pdf (OLD, basic)
2. WindowsVersion2_LODSD.pdf (OLD, using LODSD)
3. WindowsVersion3_TEST.pdf (NEW, using TEST, shorter code)
3. LinuxVersion3.pdf (NEW, using TEST, shorter code)

I assume that your code is for linux, so that's why, in this post, you can find, word "linux".
I didn't managed to test linux version, but windows version works fine.
« Last Edit: October 29, 2013, 07:49:08 AM by encryptor256 »
Encryptor256's Investigation \ Research Department.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Thanks A. K. Encryptor256! I think your "Windows version" can be built for Linux with minor (if any) changes. However, I haven't got 32-bit libc installed. "Linux Mint" is thwarting me. I don't think I can talk about it without using language unsuitable for a family forum. Two words: "Microsoft EULA". Are you kidding? This is totally unacceptable. I'm going to resystem with a 64-bit Slackware distro, and if that isn't any better I'm going back to 32-bit. I've always felt that "Ubuntu" is an African word meaning "one ring to rule them all" anyway. We've got enough of that stuff over on the "other side"! No mas!

But I wanted to point out that we can determine whether a number is odd or even by testing whether bit 0 is set or not.
Code: [Select]
test eax, 1
jnz is_odd
; or jz is _even

That's probably all I should say at the moment.

Later,
Frank


Offline new_geek

  • Jr. Member
  • *
  • Posts: 11
  • Country: 00
Hi encryptor, thank you for sharing the code, but I got a question and I need a little bit explanation. but actually more than a bit :P

This is my 32 bit number version and works too.
It add's even values into memory address dword [evenAdd].
It count's even values into memory address dword [evenCount].

1. on this part, it becomes 32 bit version since you're using dd data type right? which means double of the data of word size (16)
2. that dword [evenAdd] to declare that any value stored inside the label of evenAdd hence become dword data type? If i'm not mistaken, dword is not signed data type? is it to get its full positive value? I mean from binary 101 if it is signed it becomes negative (-3) right? but by using unsigned you would get +5?

Quote
LINUX:

Code: [Select]
.
.
.
section .text
global _start

_start:
nop

; TODO: your code here

;clearing the registers that are going to be used
xor ecx,ecx
mov ebx,dword 2
mov esi,dword list
   xor ecx,ecx >>> what is the point of using xor? why won't we use move ecx,0 instead?
   mov esi, dword list >>> what is esi?

Quote
I assume that your code is for linux, so that's why, in this post, you can find, word "linux".
I didn't managed to test linux version, but windows version works fine.

Yes, I'm using linux to do the assembly language. because time wise, school lab session is being done with linux, and I haven't learn yet how to do assembly in windows environment...

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
xor ecx,ecx >>> what is the point of using xor? why won't we use move ecx,0 instead?

There are many ways to clear out a register, some people use XOR Reg, Reg while others will use SUB Reg, Reg. Both of these are used often because MOV Reg, 0 will result with an immediate instruction which is much larger than a register instruction. It's just a bit smaller.


mov esi, dword list >>> what is esi?

ESI is one of the many general purpose registers you have at your disposal. EAX, ECX, EBX, EDX, ESI, and EDI are the ones that you'll normally use for storing values. ESI and EDI are optimized for use with indexing instructions like STOSB and MOVSB. There are also EBP and ESP registers which are mostly used for managing the stack frame (if your class hasn't gotten to this, don't worry to much about it atm, it's a hefty subject).

Also, be sure to check out Frank's post above, the problem you seem to be having is that JE doesn't mean "Jump if Even" rather it means "Jump if Equal". As Frank suggests, you can test bit 0 to find out if a number is even.
« Last Edit: October 30, 2013, 01:58:20 AM by Bryant Keller »

About Bryant Keller
bkeller@about.me

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
My previous post, updated version, you can find in that post's attachment.
See attachment named: "LinuxVersion3.pdf", there is a short and more clever version. :)

1. on this part, it becomes 32 bit version since you're using dd data type right? which means double of the data of word size (16)

Yes, you can think that! dd is double word, dd is 32bit, dd is 2x words.
+ Also im using 32 bit registers, like eax, ecx, esi, ...

2. that dword [evenAdd] to declare that any value stored inside the label of evenAdd hence become dword data type?

No it will not become dword data type.
If i type like this: dword [evenAdd], it means i will operate on 32 bit data in address evenAdd.
For example:
add dword [evenAdd],ecx
I add ecx to 32bit value at address of evenAdd.

If i'm not mistaken, dword is not signed data type?

I think all data types are unsigned, all it depends what we want to see.
Any data type CAN BE signed or unsigned.

I mean from binary 101 if it is signed it becomes negative (-3) right? but by using unsigned you would get +5?


Let book "Art of Assembly" answer your question,
this is a topic for you:
« Last Edit: October 30, 2013, 01:30:46 PM by encryptor256 »
Encryptor256's Investigation \ Research Department.