Linux Assembly - Trying to understand a jump.

dave343

[H]ard|Gawd
Joined
Oct 17, 2000
Messages
1,869
Hey all,

I recently decided I wanted to take up some programming, and have started with Linux Assembly so that's what I'm trying to teach myself. The quick answer to why is that I've been a PC Technician for years and what to change IT careers, and I have a big interest in programming. I decided to start with Assembly as I wanted to start at the bottom to understand things more, and the linux part is because that's the book I'm working through.

But, I've run into a problem that I did manage to solve (pat on back)... but I can't fully wrap my head around the solution, so hopefully someone can explain it a bit for me.

There was a program to find a maximum number, and then the author explains in detail what every step does after. At the end of the chapter, the Author has some review questions, and asks that you make the program now find the minimum.
Ok... so after a few hours (yes it took me that long) I finally figured out I needed to change a JLE (jump if the second value is less than the first), to JG (jump if the second value is greater than the first).
GREAT, so after change this value, now it finds the minimum number. But wait... since the numbers are not in lowest to highest, or highest to lowest... to me, that means the jump won't always be true. Because once the EAX register (see below) has a higher or lower number then before, and different to what is in EBX, how is it still finding the lowest?

See the code below. Hopefully someone can help explain to me how this program finds the lowest value when changing JLE to JG. I had to break down each line of the code to fully understand what was going on so I knew which line I needed to change. But it's just trying to understand my own resolution, that jump (JE) "Jump if second value is greater than the first". Since the numbers are not in order, to me the jump statement wouldn't always be true, and yet it still runs through all the numbers until it finds the correct mimimum number. Thanks in advance.

.section .data

data_items:
.long 67,34,222,45,75,54,34,44,33,22,11,66,255

.section .text

.globl _start
_start:
movl $0, %edi
movl data_items(,%edi,4), %eax
movl %eax, %ebx

start_loop:
cmpl $255, %eax
je loop_exit
incl %edi
movl data_items(,%edi,4), %eax
cmpl %ebx, %eax
jl start_loop

movl %eax, %ebx

jmp start_loop

loop_exit:
movl $1, %eax
int $0x80
 
Last edited:
Wow! Assembly, please tell me this is a funky april fools joke, because I can't even remember enough about it to read it anymore. Haven't touched it since '97. If it's not a joke, you chose a really difficult introduction to programming.
 
No joke here :) . I just wanted to start from the bottom and then work into C and then Python. I know it's a bit difficult but I'm working through a really good book, and I wanted a good understanding of how things are working before going into the higher level languages.
 
No joke here :) . I just wanted to start from the bottom and then work into C and then Python. I know it's a bit difficult but I'm working through a really good book, and I wanted a good understanding of how things are working before going into the higher level languages.

This is a bad idea, you're better off starting at the top and working down. High-level languages are easier to learn and the theory translates down well. Also, there is very little commercial application for assembler these days so you'd get more employability if you say started in HTML + JavaScript or Java (which is good because it's fairly easy but shares a lot of syntax with most other popular languages).

And as horrible as this sounds, I've found that most places won't hire programmers that don't have degrees in Computer Science, or at least the equivalent Community College Diploma. So bear it in mind that you might need to go back to school.
 
Honestly, I don't know how they do it now in school, I know they focus more on web languages/standards (JS, HTML 5.0, CSS, .Net, etc) then traditional programming languages (C/C++, Pascal, ADA, etc). When I was going to school (figures, I'm a CS w/ a job in network administration, kind of the opposite direction than what you did), they taught some form of older language such as Pascal or ADA to teach you form & function, then progressed on to C, and then learning OOP style programming later, with the idea that you wanted a base to work from. Never really used Pascal past college, but a lot of my C/C++ knowledge has gone into using PHP & JS primarily. I would skip assembly, it's really a major PITA, and not something commonly used anymore, ever since they finally complied C using C itself, which was a major breakthrough in programming. But, if you ever want a funky project, using only addition, and single byte registers, program a 4 function calculator. That was our final project, it was crazy!

On a side note: #1 lesson in programming, Document, Document, Document. If you don't learn anything about any programming language, that's okay, just please, for the love of God, Document! Otherwise, sometime later, when you or someone else looks at something, you'll have no clue what's going on. And pretty much most programmers suck at documenting, but we all appreciate it when its there, makes it so much easier to understand.
 
.

.section .data

data_items:
.long 67,34,222,45,75,54,34,44,33,22,11,66,255

.section .text

.globl _start
_start:
movl $0, %edi
movl data_items(,%edi,4), %eax
movl %eax, %ebx

start_loop:
cmpl $255, %eax
je loop_exit
incl %edi
movl data_items(,%edi,4), %eax
cmpl %ebx, %eax
jl start_loop

movl %eax, %ebx

jmp start_loop

loop_exit:
movl $1, %eax
int $0x80

Replying via phone, please excuse any typos :)
The reason this works is that there is an unconditional jump between jl start_ loop and loop_exit. %ebx stores the found value, and the only way it gets updated is if the conditional jump doesn't exit first. In pseudo code, you can think of the code as

Array = 67,34,222,45,75,54,34,44,33,22,11,66,255

I = 0

Eax=ebx=array
Loop_start:
If eax == 255 goto loop_exit
I++
eax=array
If ebx<eax goto loop_start
Ebx=eax
Goto loop_start

Loop_exit:

Eax=1
sys_exit (ebx will contain value for exit code)
 
Replying via phone, please excuse any typos :)
The reason this works is that there is an unconditional jump between jl start_ loop and loop_exit. %ebx stores the found value, and the only way it gets updated is if the conditional jump doesn't exit first. In pseudo code, you can think of the code as

Array = 67,34,222,45,75,54,34,44,33,22,11,66,255

I = 0

Eax=ebx=array
Loop_start:
If eax == 255 goto loop_exit
I++
eax=array
If ebx<eax goto loop_start
Ebx=eax
Goto loop_start

Loop_exit:

Eax=1
sys_exit (ebx will contain value for exit code)


Many thanks!
 
This is a bad idea, you're better off starting at the top and working down. High-level languages are easier to learn and the theory translates down well. Also, there is very little commercial application for assembler these days so you'd get more employability if you say started in HTML + JavaScript or Java (which is good because it's fairly easy but shares a lot of syntax with most other popular languages).

And as horrible as this sounds, I've found that most places won't hire programmers that don't have degrees in Computer Science, or at least the equivalent Community College Diploma. So bear it in mind that you might need to go back to school.

I thought about starting at the top, but honestly I really wanted a good understand of what's going on at the bottom. Learning programming is first and formost a personal goal as a pure programming job isn't what I'm after. The end goal is just becoming proficient in Python/C, for now.
And as for school I already have a 3 year diploma in computer studies.
 
Last edited:
Back
Top